sed to do replace on line above

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?

Any suggestion welecome! :slight_smile:

What is your search pattern? BBBB? Give a more realistic sample input and expected output...

--ahamed

Ok, say I have this piece of XML:

<TERM>
<PARAM NAME="DATACENTER� OP="EQ"
VALUE="Data1"/>
<PARAM NAME="GROUP� OP="EQ� VALUE="GRP_04"/>
</TERM>
</TERMS>

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

Try this...

awk '/VALUE="Data1"\/>/{x=$0;getline;if($0~srch){print "whatever you want"}else{print x}}1' 
srch='<PARAM NAME="GROUP� OP="EQ� VALUE="GRP_04"/>' input_file

--ahamed

Hello - thanks that works for the specific example. However I probably should have explained that the value above won't alway be:

VALUE="Data1"/>

it could be anything. That is why I want to search on:

<PARAM NAME="GROUP� OP="EQ� VALUE="GRP_04"/>

and replace the line above whatever it is.

Any further suggestions? Thanks
[/SIZE]

Try:

awk '{if(/CCCCCCC/)print t;else if(p)print p;p=$0}END{print p}' t=XXX infile

Scrutinizer, thats perfect thank you. ahamed101 thanks for some good input.:slight_smile:

i tried scrutinizer one liner.. but i am getting error..
sorry but i am new to awk.. is it the complete command or just the syntax

Are you on Solaris? If so use /usr/xpg4/bin/awk or nawk instead of awk

1 Like

wow it worked.. but whats the difference between

/usr/bin/awk

and

/usr/xpg4/bin/awk

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.

1 Like

Just for the record: a sed solution would be:

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