Help with awk and particular named column print

Hi Unix Gurus,

I have some 145+ xml in some folder and I want just loadOperation to be printed. Sample o/p:

 file1.xml loadOperation="InsertOrUpdate">
 file2.xml loadOperation="Update">
 file3.xml loadOperation="Update">
 file4.xml loadOperation="Append">

In some of them, in the 2nd line, last column is loadOperation; but in some of them last column is sourceTable. But in those cases, 2nd last column is always loadOperation. Sample given:
--Last column is NOT loadOperation

<DataMapping123 xmlns:xsd="http://www.w3.org/2001/XMLSchema-instance" xsd:noNamespaceSchemaLocation="DataMappping123.xsd" name="fileOperation_1_type" table="XYZ" loadOperation="Stage" sourceTable="ABC">

--Last column is loadOperation

<DataMapping533 xmlns:xsd="http://www.w3.org/2001/XMLSchema-instance" xsd:noNamespaceSchemaLocation="DataMappping533.xsd" name="fileOperation_15_type" table="XYZ" loadOperation="Append">

So I tried the below:
1st with direct $NF print, not getting desired output though:

head -2 fileName2.xml|tail -1 |awk '{print $NF}'

2nd with conditional statement, still not printing the desired column ( I guess I'm not able to do partial match):

$ head -2 fileName1.xml|tail -1 |awk -v col=loadOperation '{if ($NF==col) {print $NF} else {print $(NF-1)}}'

my final code will be with for command as below:

for logTime in `ls *.xml`; do logTailString=`head -2 $logTime|tail -1 |<<here goes the proper awk script>>; echo "$1 $logTime $logTailString"; done

Any type of suggestions welcome. I'm open for ideas.

Cheers,
Sapy.

Try (untested)

awk 'FNR == 2 {print FILENAME, ($NF ~ /^load/)?$NF:$(NF-1)}' *.xml
1 Like

Thank you @RudiC, It's worked like a charm.

Hello spas19,

If in case you have more number of files than you could try following.

for file in *.xml
do
    awk 'FNR == 2 {print FILENAME, ($NF ~ /^load/)?$NF:$(NF-1)}'  $file
done

Thanks,
R. Singh