sed fails to remove newline character

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.

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

sed delimits on newlines – they are always removed on input and reinserted on output. There is never a newline 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

Method 2

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

Method 3

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

Method 4

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

Method 5

To remove only the last newline, pipe through:

sed -z '$ s/n$//'

sed won’t add a to then end of the stream if the delimiter is set to NUL via -z, whereas to create a POSIX text file (defined to end in a n), it will always output a final n without -z.

Eg:

$ { echo foo; echo bar; } | sed -z '$ s/n$//'; echo tender
foo
bartender

And to prove no NUL added:

$ { echo foo; echo bar; } | sed -z '$ s/n$//' | xxd
00000000: 666f 6f0a 6261 72                        foo.bar

To remove multiple trailing newlines, pipe through:

sed -Ez '$ s/n+$//'


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x