This command runs fine:

$ sed  -e '/foo/{g; d}' myfile   

But this one has an error:

$ sed  -e '/foo/{g; a bar}' myfile
sed: -e expression #1, char 0: unmatched `{'

What's wrong with it?

link|improve this question
@TechZilla: I tried to write some composite command inside {}. Using g; is just some substitute. – Qiang Li Dec 31 '11 at 4:16
In which manner do you mean composite command? – TechZilla Dec 31 '11 at 4:27
For example, can I do {d;a bar} if you think {g;a bar} is nonsensical. – Qiang Li Dec 31 '11 at 4:41
they both are nonsensical, when you do {g; you are referring to spacing. The second letter clarifies the changes. What would the bar do?. And if the d; does something, which it might, I'm not sure what it is. But invoking anything in { is the very uncommon to say the least. If you want to do any of the 'normal' uses of sed, like working with in regexp, you wouldn't use any {} – TechZilla Dec 31 '11 at 5:51
... You meant like d\, for deleting a line, a\ add line? I updated my answer. – TechZilla Dec 31 '11 at 6:04
show 2 more comments
feedback

1 Answer

The a statement in sed requires a actual newline character (Cntl-j), not an embedded carriage return (Ctrl-m). So the only way to really get the a statement to work is:

sed '/foo/{g;a\
bar
}'

You need to have the newline after the line of input. If you want more than one line, then quote the newline:

sed '/foo/{g;a\
bar\
xyzzy
}'

This goes back to the roots of sed, which is ed.

link|improve this answer
Your right, I pointed you. – TechZilla Dec 31 '11 at 6:14
feedback

Your Answer

 
or
required, but never shown

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