sed fails to remove newline character

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

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


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

Tags:

Sed

Newlines