Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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//
}
share|improve this question

1 Answer 1

up vote 2 down vote accepted

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//}'
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.