Using gsub with xml formats

Hello,

I'm looking again for your precious help.

I'm running an IBM AIX box.
I have 'wrong' xml files and waiting for the bug correction I need to manipulate them
The problem is a missing tag.
Now you know that xmls are full of < and > and \ so our scripts just go beserk.
To be on the safe side I want to insert a tag between two known tags:

wrong data

<PackDate>20160615</PackDate>
<TMCCode>BA</TMCCode>
<FullPallet>N</FullPallet>
<BlockCode>0</BlockCode>
<AdmQualifier>568U#3-335</AdmQualifier>

correct data

<PackDate>20160615</PackDate>
<TMCCode>BA</TMCCode>
<FullPallet>N</FullPallet>
<AviExp>123456</AviExp>
<BlockCode>0</BlockCode>
<AdmQualifier>568U#3-335</AdmQualifier>

Instead of using while...read... I tried gsub, but all those fancy delimiters just don't let the command to work properly. Furthermore there are CRLF that are to be taken into account.

This is my try:

awk '{gsub("<FullPallet>N</FullPallet>\n<BlockCode>0</BlockCode>","<FullPallet>N</FullPallet>\n<AviExp>123456</AviExp>\n<BlockCode>0</BlockCode>");printf"%s",$0}' S210404890.xml > test.txt

Any help is really appreciated.

thanks,
Ema

Hello emare,

Could you please try following and let me know how it goes then.

awk '{gsub(/<\/FullPallet>/,"</FullPallet>\n<AviExp>123456</AviExp>",$0)} 1'  Input_file

Output will be as follows.

<PackDate>20160615</PackDate>
<TMCCode>BA</TMCCode>
<FullPallet>N</FullPallet>
<AviExp>123456</AviExp>
<BlockCode>0</BlockCode>
<AdmQualifier>568U#3-335</AdmQualifier>

Thanks,
R. Singh

Thanks RavinderSingh, it went OK
But I need to check both tags <FullPallet>.... and <BlockCode>.... because the tag <AviExp>.... can already be there and so I must not add it twice.

awk works linewise unless you'd redefine the record separator. Which you don't. So your try cannot work like posted. On top, you're talking of CRLF chars but there are none in the input sample posted.

Howsoever, try

awk '
/<FullPallet>/  {FP = 1}
/<AviExp>/      {FP = 0}
FP &&
/<BlockCode>/   {print "<AviExp>123456</AviExp>"}
1
' file
<PackDate>20160615</PackDate>
<TMCCode>BA</TMCCode>
<FullPallet>N</FullPallet>
<AviExp>123456</AviExp>
<BlockCode>0</BlockCode>
<AdmQualifier>568U#3-335</AdmQualifier>