1

I am taking efforts to learn sed and encounter such a situation

$ echo "abcd" | sed -n "/b/p"
abcd

it works properly,

$ echo "abcd" | sed -n "
→ /b/p"
abcd

good again, but

$ echo "Abcd" | sed -n "/b
/p"
sed: -e expression #1, char 2: unterminated address regex

What's the problem with the error report?

1

/b/p is a complete sed command. Each sed command needs to be on a single line. When you say /b on one line, sed treats it as a syntax error. You can try by

echo "abcd" | sed -n "/b"

and see the result. You can also try

echo "abcd" | sed -n "/b/p
> p"

and observe that after the command is completed, the next command takes effect.

1

sed patterns are fundamentally line-based, so it expects the second slash to be on the same line as the first one. From info sed:

Commands within a SCRIPT or SCRIPT-FILE can be separated by semicolons (';') or newlines (ASCII 10).

1

Most commands in Linux are fundamentally line-based, so it expects the second slash to be on the same line as the first one. In your case to work for 3rd one you can use the below

$ echo "Abcd" | sed -n "/b\
 /p"
 Abcd
  • With "line based", one usually means that the data is read in a line based manner. Program code is usually multi-line. – Kusalananda Oct 30 '18 at 10:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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