Solved How to append a line using sed after a match

I've spent several hours trying to insert/append a line after a pattern match following numerous examples, but can't get any of them to work... not sure if this is because of differences between FreeBSD sed and GNU sed..

I'm trying to append a line after a match, rather than inserting text straight after the match.

I'd appreciate an example of doing this properly, if someone could assist.
 
not sure if this is because of differences between FreeBSD sed and GNU sed.
It is. On GNU sed you could do:
Code:
cat << EOF > users
martin
peter
EOF
and append line after martin by sed -i '/^martin/a balanga' users.
On FreeBSD I'd have to google myself :| (or follow covacat's idea:) )
 
Command:
$ echo "This is not
it
is it?" | sed -n 'p
/it/i\
FOUND "it"
'


'echo' output (without pipe into sed filter):
This is not
it
is it?


Output (with sed filter):
This is not
it
FOUND "it"
is it?
FOUND "it"


sed flags: -n: Don't auto-print lines

Commented sed program: (inside the '-marks on the command line)
Code:
p          # Print the line; use with -n so we can act _after_ echoing the line.
/it/i\     # If the (just printed) line contains 'it'; also print the following.
FOUND "it" # Text to append after found 'it'; NOTE blank line below for newline.
 
or if you want to match whole line and add a new line after
sed "/foo/G" will do
Code:
~$cat x
aaa
bbb
foo
ccc
baz
ddd
~$cat x |sed  '/foo/G'
aaa
bbb
foo

ccc
baz
ddd
 
Just thought I'd post the script I managed to come up with after many, many hours of trial and error:-

Bash:
sed -E -i "" -e "/^.getopt/a\\
        \/usr\/local\/bin\/getopt -o t --long test -- --test \| grep '\^ \*--test \*--', \\\\" include/prereq-build.mk
 
Back
Top