sed fails to remove newline character2019 Community Moderator ElectionHow to remove multiple blank lines from a file?Why the inconsistency with using cat vs. echo piped to this sed command?Remove backslash + newline sequencesHow can I make sed not append a newline character?Remove the exact same matching lines with sedArray from piped commands failsusing sed to modify all files in a directory and name the outputs accordinglysed behaves strange on android tabletHow to create a folder from a string in a file?Scripting id3 tags with id3v2 and sed
Motivation for Zeta Function of an Algebraic Variety
Virginia employer terminated employee and wants signing bonus returned
Is it "Vierergruppe" or "Viergruppe", or is there a distinction?
NASA's RS-25 Engines shut down time
Are all players supposed to be able to see each others' character sheets?
Do f-stop and exposure time perfectly cancel?
What was the Kree's motivation in Captain Marvel?
Could you please stop shuffling the deck and play already?
How can I ensure my trip to the UK will not have to be cancelled because of Brexit?
List elements digit difference sort
Are tamper resistant receptacles really safer?
How is the wildcard * interpreted as a command?
Hotkey (or other quick way) to insert a keyframe for only one component of a vector-valued property?
Are there historical instances of the capital of a colonising country being temporarily or permanently shifted to one of its colonies?
What are some noteworthy "mic-drop" moments in math?
They call me Inspector Morse
Declaring and defining template, and specialising them
Counting all the hearts
How to draw cubes in a 3 dimensional plane
Error during using callback start_page_number in lualatex
UART pins to unpowered MCU?
Why does liquid water form when we exhale on a mirror?
Word for a person who has no opinion about whether god exists
What does "the touch of the purple" mean?
sed fails to remove newline character
2019 Community Moderator ElectionHow to remove multiple blank lines from a file?Why the inconsistency with using cat vs. echo piped to this sed command?Remove backslash + newline sequencesHow can I make sed not append a newline character?Remove the exact same matching lines with sedArray from piped commands failsusing sed to modify all files in a directory and name the outputs accordinglysed behaves strange on android tabletHow to create a folder from a string in a file?Scripting id3 tags with id3v2 and sed
I've been using sed
for quite some time but here is a quirk I came around with, which I am not able to resolve.
Let me explain my problem with the actual case.
Scene#1
printf "ls" | xclip -selection clipboard
echo "ls" | xclip -selection clipboard
In the first command, I pipe printf
output to xclip
so that it gets copied to the clipboard. Now, printf
, unlike echo
does not insert a new line at the end by default. So, if I paste this content into terminal, the ls
command that is copied does not automatically run.
In the second, there is a new line at the end, so pasting the clipboard content also results in the running of the command in the clipboard.
This is undesirable for me. So, I wanted to remove the newline using sed
, but it failed, as explained in the scene below.
Scene#2
echo "ls" | sed -r 's/n//g' | xclip -selection clipboard
The content in the clipboard still contains new-line. When I paste it into terminal, the command automatically runs.
I also tried removing carriage return character r
. But nada. It seems I am missing something very crucial/basic here.
sed newlines
add a comment |
I've been using sed
for quite some time but here is a quirk I came around with, which I am not able to resolve.
Let me explain my problem with the actual case.
Scene#1
printf "ls" | xclip -selection clipboard
echo "ls" | xclip -selection clipboard
In the first command, I pipe printf
output to xclip
so that it gets copied to the clipboard. Now, printf
, unlike echo
does not insert a new line at the end by default. So, if I paste this content into terminal, the ls
command that is copied does not automatically run.
In the second, there is a new line at the end, so pasting the clipboard content also results in the running of the command in the clipboard.
This is undesirable for me. So, I wanted to remove the newline using sed
, but it failed, as explained in the scene below.
Scene#2
echo "ls" | sed -r 's/n//g' | xclip -selection clipboard
The content in the clipboard still contains new-line. When I paste it into terminal, the command automatically runs.
I also tried removing carriage return character r
. But nada. It seems I am missing something very crucial/basic here.
sed newlines
add a comment |
I've been using sed
for quite some time but here is a quirk I came around with, which I am not able to resolve.
Let me explain my problem with the actual case.
Scene#1
printf "ls" | xclip -selection clipboard
echo "ls" | xclip -selection clipboard
In the first command, I pipe printf
output to xclip
so that it gets copied to the clipboard. Now, printf
, unlike echo
does not insert a new line at the end by default. So, if I paste this content into terminal, the ls
command that is copied does not automatically run.
In the second, there is a new line at the end, so pasting the clipboard content also results in the running of the command in the clipboard.
This is undesirable for me. So, I wanted to remove the newline using sed
, but it failed, as explained in the scene below.
Scene#2
echo "ls" | sed -r 's/n//g' | xclip -selection clipboard
The content in the clipboard still contains new-line. When I paste it into terminal, the command automatically runs.
I also tried removing carriage return character r
. But nada. It seems I am missing something very crucial/basic here.
sed newlines
I've been using sed
for quite some time but here is a quirk I came around with, which I am not able to resolve.
Let me explain my problem with the actual case.
Scene#1
printf "ls" | xclip -selection clipboard
echo "ls" | xclip -selection clipboard
In the first command, I pipe printf
output to xclip
so that it gets copied to the clipboard. Now, printf
, unlike echo
does not insert a new line at the end by default. So, if I paste this content into terminal, the ls
command that is copied does not automatically run.
In the second, there is a new line at the end, so pasting the clipboard content also results in the running of the command in the clipboard.
This is undesirable for me. So, I wanted to remove the newline using sed
, but it failed, as explained in the scene below.
Scene#2
echo "ls" | sed -r 's/n//g' | xclip -selection clipboard
The content in the clipboard still contains new-line. When I paste it into terminal, the command automatically runs.
I also tried removing carriage return character r
. But nada. It seems I am missing something very crucial/basic here.
sed newlines
sed newlines
edited Jan 25 '15 at 0:00
Gilles
541k12810951611
541k12810951611
asked Jan 24 '15 at 23:52
shivamsshivams
2,94111426
2,94111426
add a comment |
add a comment |
4 Answers
4
active
oldest
votes
sed
delimits on n
ewlines - they are always removed on input and reinserted on output. There is never a n
ewline character in a sed
pattern space which did not occur as a result of an edit you have made.
Note: with the exception of GNU sed
's -z
mode...
Just use tr
:
echo ls | tr -d \n | xclip -selection clipboard
Or, better yet, forget sed
altogether:
printf ls | xclip -selection clipboard
add a comment |
Many text processing tools, including sed, operate on the content of the line, excluding the newline character. The first thing sed does when processing a line is to strip off the newline at the end, then it executes the commands in the script, and it adds a final newline when printing out. So you won't be able to remove the newline with sed.
To remove all the newlines, you can use tr
instead:
echo "ls" | tr -d 'n' | xclip
add a comment |
If you are just putting commands in the clipboard
echo -n "ls " | xclip -selection clipboard
If you additionally need to make more complex transformations,
echo "ls " | perl -pe 's/n//' | xclip -selection clipboard
Hmm. Smooth solution. I guess it is perhaps time to move on to perl or python for regex.
– shivams
Jan 25 '15 at 0:49
2
@shivams -perl
andpython
are both much slower thansed
can be when used correctly. Learning to use it correctly is the key, though.
– mikeserv
Jan 25 '15 at 1:26
@mikeserv - I agree; special for oneliner sed is wonderful. If the transformations are going to become more complex, then I prefer perl/python/gawk.
– JJoao
Jan 25 '15 at 12:35
@JJoa: Your preference is your own, butsed
can do far more than handle just your one-liners, and it can likely do so faster than any of those other tools you mention.
– mikeserv
Jan 25 '15 at 14:54
1
@mikeserv - once again, I agree with you. Some year ago I had a translation system EN-PT written in sed (20000 s/.../.../), generated by sed! And it worked! Later it became Perl, to have arrays, hashes, functions, packages and CPAN.
– JJoao
Jan 25 '15 at 21:10
add a comment |
You can replace newlines in sed by passing it the -z option.
sed -z 's/n/ /g'
Sed man page:
-z, --null-data separate lines by NUL characters
New contributor
add a comment |
Your Answer
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "106"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f180901%2fsed-fails-to-remove-newline-character%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
4 Answers
4
active
oldest
votes
4 Answers
4
active
oldest
votes
active
oldest
votes
active
oldest
votes
sed
delimits on n
ewlines - they are always removed on input and reinserted on output. There is never a n
ewline character in a sed
pattern space which did not occur as a result of an edit you have made.
Note: with the exception of GNU sed
's -z
mode...
Just use tr
:
echo ls | tr -d \n | xclip -selection clipboard
Or, better yet, forget sed
altogether:
printf ls | xclip -selection clipboard
add a comment |
sed
delimits on n
ewlines - they are always removed on input and reinserted on output. There is never a n
ewline character in a sed
pattern space which did not occur as a result of an edit you have made.
Note: with the exception of GNU sed
's -z
mode...
Just use tr
:
echo ls | tr -d \n | xclip -selection clipboard
Or, better yet, forget sed
altogether:
printf ls | xclip -selection clipboard
add a comment |
sed
delimits on n
ewlines - they are always removed on input and reinserted on output. There is never a n
ewline character in a sed
pattern space which did not occur as a result of an edit you have made.
Note: with the exception of GNU sed
's -z
mode...
Just use tr
:
echo ls | tr -d \n | xclip -selection clipboard
Or, better yet, forget sed
altogether:
printf ls | xclip -selection clipboard
sed
delimits on n
ewlines - they are always removed on input and reinserted on output. There is never a n
ewline character in a sed
pattern space which did not occur as a result of an edit you have made.
Note: with the exception of GNU sed
's -z
mode...
Just use tr
:
echo ls | tr -d \n | xclip -selection clipboard
Or, better yet, forget sed
altogether:
printf ls | xclip -selection clipboard
answered Jan 25 '15 at 0:00
mikeservmikeserv
45.9k668160
45.9k668160
add a comment |
add a comment |
Many text processing tools, including sed, operate on the content of the line, excluding the newline character. The first thing sed does when processing a line is to strip off the newline at the end, then it executes the commands in the script, and it adds a final newline when printing out. So you won't be able to remove the newline with sed.
To remove all the newlines, you can use tr
instead:
echo "ls" | tr -d 'n' | xclip
add a comment |
Many text processing tools, including sed, operate on the content of the line, excluding the newline character. The first thing sed does when processing a line is to strip off the newline at the end, then it executes the commands in the script, and it adds a final newline when printing out. So you won't be able to remove the newline with sed.
To remove all the newlines, you can use tr
instead:
echo "ls" | tr -d 'n' | xclip
add a comment |
Many text processing tools, including sed, operate on the content of the line, excluding the newline character. The first thing sed does when processing a line is to strip off the newline at the end, then it executes the commands in the script, and it adds a final newline when printing out. So you won't be able to remove the newline with sed.
To remove all the newlines, you can use tr
instead:
echo "ls" | tr -d 'n' | xclip
Many text processing tools, including sed, operate on the content of the line, excluding the newline character. The first thing sed does when processing a line is to strip off the newline at the end, then it executes the commands in the script, and it adds a final newline when printing out. So you won't be able to remove the newline with sed.
To remove all the newlines, you can use tr
instead:
echo "ls" | tr -d 'n' | xclip
edited Jan 25 '15 at 18:42
JJoao
7,3591928
7,3591928
answered Jan 25 '15 at 0:03
GillesGilles
541k12810951611
541k12810951611
add a comment |
add a comment |
If you are just putting commands in the clipboard
echo -n "ls " | xclip -selection clipboard
If you additionally need to make more complex transformations,
echo "ls " | perl -pe 's/n//' | xclip -selection clipboard
Hmm. Smooth solution. I guess it is perhaps time to move on to perl or python for regex.
– shivams
Jan 25 '15 at 0:49
2
@shivams -perl
andpython
are both much slower thansed
can be when used correctly. Learning to use it correctly is the key, though.
– mikeserv
Jan 25 '15 at 1:26
@mikeserv - I agree; special for oneliner sed is wonderful. If the transformations are going to become more complex, then I prefer perl/python/gawk.
– JJoao
Jan 25 '15 at 12:35
@JJoa: Your preference is your own, butsed
can do far more than handle just your one-liners, and it can likely do so faster than any of those other tools you mention.
– mikeserv
Jan 25 '15 at 14:54
1
@mikeserv - once again, I agree with you. Some year ago I had a translation system EN-PT written in sed (20000 s/.../.../), generated by sed! And it worked! Later it became Perl, to have arrays, hashes, functions, packages and CPAN.
– JJoao
Jan 25 '15 at 21:10
add a comment |
If you are just putting commands in the clipboard
echo -n "ls " | xclip -selection clipboard
If you additionally need to make more complex transformations,
echo "ls " | perl -pe 's/n//' | xclip -selection clipboard
Hmm. Smooth solution. I guess it is perhaps time to move on to perl or python for regex.
– shivams
Jan 25 '15 at 0:49
2
@shivams -perl
andpython
are both much slower thansed
can be when used correctly. Learning to use it correctly is the key, though.
– mikeserv
Jan 25 '15 at 1:26
@mikeserv - I agree; special for oneliner sed is wonderful. If the transformations are going to become more complex, then I prefer perl/python/gawk.
– JJoao
Jan 25 '15 at 12:35
@JJoa: Your preference is your own, butsed
can do far more than handle just your one-liners, and it can likely do so faster than any of those other tools you mention.
– mikeserv
Jan 25 '15 at 14:54
1
@mikeserv - once again, I agree with you. Some year ago I had a translation system EN-PT written in sed (20000 s/.../.../), generated by sed! And it worked! Later it became Perl, to have arrays, hashes, functions, packages and CPAN.
– JJoao
Jan 25 '15 at 21:10
add a comment |
If you are just putting commands in the clipboard
echo -n "ls " | xclip -selection clipboard
If you additionally need to make more complex transformations,
echo "ls " | perl -pe 's/n//' | xclip -selection clipboard
If you are just putting commands in the clipboard
echo -n "ls " | xclip -selection clipboard
If you additionally need to make more complex transformations,
echo "ls " | perl -pe 's/n//' | xclip -selection clipboard
edited Jan 25 '15 at 12:24
answered Jan 25 '15 at 0:42
JJoaoJJoao
7,3591928
7,3591928
Hmm. Smooth solution. I guess it is perhaps time to move on to perl or python for regex.
– shivams
Jan 25 '15 at 0:49
2
@shivams -perl
andpython
are both much slower thansed
can be when used correctly. Learning to use it correctly is the key, though.
– mikeserv
Jan 25 '15 at 1:26
@mikeserv - I agree; special for oneliner sed is wonderful. If the transformations are going to become more complex, then I prefer perl/python/gawk.
– JJoao
Jan 25 '15 at 12:35
@JJoa: Your preference is your own, butsed
can do far more than handle just your one-liners, and it can likely do so faster than any of those other tools you mention.
– mikeserv
Jan 25 '15 at 14:54
1
@mikeserv - once again, I agree with you. Some year ago I had a translation system EN-PT written in sed (20000 s/.../.../), generated by sed! And it worked! Later it became Perl, to have arrays, hashes, functions, packages and CPAN.
– JJoao
Jan 25 '15 at 21:10
add a comment |
Hmm. Smooth solution. I guess it is perhaps time to move on to perl or python for regex.
– shivams
Jan 25 '15 at 0:49
2
@shivams -perl
andpython
are both much slower thansed
can be when used correctly. Learning to use it correctly is the key, though.
– mikeserv
Jan 25 '15 at 1:26
@mikeserv - I agree; special for oneliner sed is wonderful. If the transformations are going to become more complex, then I prefer perl/python/gawk.
– JJoao
Jan 25 '15 at 12:35
@JJoa: Your preference is your own, butsed
can do far more than handle just your one-liners, and it can likely do so faster than any of those other tools you mention.
– mikeserv
Jan 25 '15 at 14:54
1
@mikeserv - once again, I agree with you. Some year ago I had a translation system EN-PT written in sed (20000 s/.../.../), generated by sed! And it worked! Later it became Perl, to have arrays, hashes, functions, packages and CPAN.
– JJoao
Jan 25 '15 at 21:10
Hmm. Smooth solution. I guess it is perhaps time to move on to perl or python for regex.
– shivams
Jan 25 '15 at 0:49
Hmm. Smooth solution. I guess it is perhaps time to move on to perl or python for regex.
– shivams
Jan 25 '15 at 0:49
2
2
@shivams -
perl
and python
are both much slower than sed
can be when used correctly. Learning to use it correctly is the key, though.– mikeserv
Jan 25 '15 at 1:26
@shivams -
perl
and python
are both much slower than sed
can be when used correctly. Learning to use it correctly is the key, though.– mikeserv
Jan 25 '15 at 1:26
@mikeserv - I agree; special for oneliner sed is wonderful. If the transformations are going to become more complex, then I prefer perl/python/gawk.
– JJoao
Jan 25 '15 at 12:35
@mikeserv - I agree; special for oneliner sed is wonderful. If the transformations are going to become more complex, then I prefer perl/python/gawk.
– JJoao
Jan 25 '15 at 12:35
@JJoa: Your preference is your own, but
sed
can do far more than handle just your one-liners, and it can likely do so faster than any of those other tools you mention.– mikeserv
Jan 25 '15 at 14:54
@JJoa: Your preference is your own, but
sed
can do far more than handle just your one-liners, and it can likely do so faster than any of those other tools you mention.– mikeserv
Jan 25 '15 at 14:54
1
1
@mikeserv - once again, I agree with you. Some year ago I had a translation system EN-PT written in sed (20000 s/.../.../), generated by sed! And it worked! Later it became Perl, to have arrays, hashes, functions, packages and CPAN.
– JJoao
Jan 25 '15 at 21:10
@mikeserv - once again, I agree with you. Some year ago I had a translation system EN-PT written in sed (20000 s/.../.../), generated by sed! And it worked! Later it became Perl, to have arrays, hashes, functions, packages and CPAN.
– JJoao
Jan 25 '15 at 21:10
add a comment |
You can replace newlines in sed by passing it the -z option.
sed -z 's/n/ /g'
Sed man page:
-z, --null-data separate lines by NUL characters
New contributor
add a comment |
You can replace newlines in sed by passing it the -z option.
sed -z 's/n/ /g'
Sed man page:
-z, --null-data separate lines by NUL characters
New contributor
add a comment |
You can replace newlines in sed by passing it the -z option.
sed -z 's/n/ /g'
Sed man page:
-z, --null-data separate lines by NUL characters
New contributor
You can replace newlines in sed by passing it the -z option.
sed -z 's/n/ /g'
Sed man page:
-z, --null-data separate lines by NUL characters
New contributor
New contributor
answered 4 hours ago
JSON C11JSON C11
1013
1013
New contributor
New contributor
add a comment |
add a comment |
Thanks for contributing an answer to Unix & Linux Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f180901%2fsed-fails-to-remove-newline-character%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
-newlines, sed