I want to do two things by sed
when matching a pattern:
Such as the original content in a text file is:
abc
123
edf
I want to replace 123
to XXX
and append YYY
after the line:
abc
XXX
YYY
edf
I tried to do this by sed '/123/{s/123/XXX;a\YYY\}'
, but it gave an error: Unmatched "{"
.
It seemed that command a
treats all characters after it as text to append. So how can I make a
know the end position of the text for append?
This might work for you (GNU sed):
sed '/123/c\XXX\nYYY' file
This uses the c
command to change the line matched by the pattern.
Or if you prefer to substitute and append:
sed 's/123/XXX/;T;a\YYY' file
Or:
sed -e '/123/{s//XXX/;a\YYY' -e '}' file
Or:
sed $'/123/{s//XXX/;a\YYY\n}' file