Hello,
I need to use sed to find a certain string and then do a replace on the line above. For example if I had the following text:
AAAAAAA
BBBBBBB
CCCCCCC
DDDDDD
I would like to find CCCCCCC , but I would like the replace to be carried out on BBBBBBB . I have done some searching and can only find instructions on deleting and inserting on lines other than the ones for which you are matching.
Do I need a delete and then a second sed for an insert or can it all be done in one sed?
I need to find <PARAM NAME="GROUP� OP="EQ� VALUE="GRP_04"/>
but update VALUE="Data1"/> (i.e. the line above)
I specifically have to only have to update:
VALUE="Data1"/> when it is followed by <PARAM NAME="GROUP� OP="EQ� VALUE="GRP_04"/>
, because there are many instances of VALUE="Data1"/> that I do want to update in the XML, does that make sense?
in short I need to have a command to replace the line above my search string
Hi, /usr/xpg4/bin/awk is the POSIX (standard) version of awk on Solaris. The default awk version on Solaris (/usr/bin/awk) is outdated and is best avoided.
sed '/pattern/!{x;1d};/pattern/{x;s/old/new/;n;x};${p;x}' infile
Where 'pattern' is what you want to match on; and s/old/new/ is what you want to substitute with in the previous line.
Commands explained:
sed '
/pattern/!{x;1d}; #line doesn't contain "pattern". Exchange the pattern buffer with hold buffer (x)
#and if it's the first line, delete (otherwise an empty line is printed at the beginning)
/pattern/{x; s/old/new/; n;x}; #pattern found; exchange pat and hold buffs (x),
#do substitution (s),
#print current pattern space (changed line) and take a new line (n),
#and exchange the new line with the line stashed in hold buffer (x)
${p;x} #print the pattern space and the hold space on the last line
' infile