I'd like to search for lines that have fudge
and place them at the end of the previous line.
Could someone explain to me why the following works in vim but not with sed? (i.e. if I open the file called test
with vim and key in %s/\(.*\)\n\(fudge\)/\1 \2/g
it does what I want it to).
echo -e 'foo bar baz\nfudge' > test
sed -e 's/\(.*\)\n\(fudge\)/\1 \2/g' test
sed reads the input 1 line at a time by default so there is no newline in the default single-line buffer that sed is working on at any given time, but you can used GNU sed for -z
to instead read the whole file into memory at once:
$ printf 'foo bar baz\nfudge\n' > file
$ sed -Ez 's/(.*)\n(fudge)/\1 \2/' file
foo bar baz fudge
I added -E
to enable EREs just to reduce the escaping.