Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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?

share|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

1 Answer 1

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.

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

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.