Linux – Sed error “sed: -e expression #1, char [#]: extra characters after command”

command linelinuxsed

sed '/\r$/ {N s/\n//}'
sed: -e expression #1, char 10: extra characters after command

I want to understand what is wrong with my command line.
When I can do the some of following in a script but not in a command line.

I have been working through the tutorial at www.grymoire.com. When I got stuck at the section
Working with Multiple Lines. These script works fine.

#!/bin/sh
sed '
# look for a "#" at the end of the line
/#$/ {
# Found one - now read in the next line
    N
# delete the "#" and the new line character, 
    s/#\n//
}' file

In my case I am looking for \r or return ^M.

#!/bin/sh
sed '
# look for a "#" at the end of the line
/\r$/ {
# Found one - now read in the next line
    N
# delete the "#" and the new line character, 
    s/\r\n//
}' file

The problem is when I try to convert it to a single line shell command it gives me an errors.

sed '/\r$/ {N s/\n//}'
sed: -e expression #1, char 10: extra characters after command
sed '/\r$/{N s/\n//}'
sed: -e expression #1, char 9: extra characters after command
sed '/\r$/{Ns/\n//}'
sed: -e expression #1, char 8: extra characters after command

I want to understand what is wrong with my command line.
When I can do the following in a script but not in a command line.

I have created a sed script to do the work I want.

cat test.sed 
#!/bin/sed -f
/\r$/ {
N 
s/\n//
}

Best Answer

You need to use a semicolon ; to separate sed commands on the same line. Specifically, between the N and s. No semicolon is needed when there is a newline which is why your script form works.

change

sed '/\r$/ {N s/\n//}'  

to

sed '/\r$/ {N; s/\n//}'
             ^
Related Question