perl - Use sed to replace word in 2-line pattern -
i try use sed replace word in 2-line pattern word. when in 1 line pattern 'macro "something"' found in next line replace 'block' 'core'. "something" put reference , printed out well.
my input data:
macro abcd class block ; symmetry x y ;
desired outcome:
macro abcd class core ; symmetry x y ;
my attempt in sed far:
sed 's/macro \([a-za-z0-9]*\)/,/ class block ;/macro \1\n class core ;/g' input.txt
the above did not work giving message:
sed: -e expression #1, char 30: unknown option `s'
what missing?
i'm open one-liner solutions in perl well.
thanks, gert
using perl one-liner in slurp mode:
perl -0777 -pe 's/macro \w+\n class \kblock ;/core ;/g' input.txt
or using streaming example:
perl -pe ' s/^\s*\bclass \kblock ;/core ;/ if $prev; $prev = $_ =~ /^macro \w+$/ ' input.txt
explanation:
switches:
-0777
: slurp files whole-p
: createswhile(<>){...; print}
loop each line in input file.-e
: tellsperl
execute code on command line.
Comments
Post a Comment