I have two files, how should i compare these files using shell and awk script

≯℡__Kan透↙ 提交于 2019-12-20 07:28:54

问题


I have two files

Content of file A

paybackFile_537214-760887_000_20120801.xml
paybackFile_354472-544899_000_20120801.xml
paybackFile_62-11033_000_20120801.xml
paybackFile_831669-837544_000_20120801.xml
===========================================
Total file(s) - 4
===========================================

Content of file B

14/08/2012 12:36:01: MSG: File paybackFile_537214-760887_000_20120801.xml.gpg decrypted successfully.
13/08/2012 11:36:01: MSG: File paybackFile_62-11033_000_20120801.xml.gpg not decrypted successfully.

Here i have names of .xml files. From file A we check that **.xml file is present in file B and also check whether it has been decrypted successfully.

Could you please help me with this.

Thanks in advance.

Regards, Smita


回答1:


awk 'FNR==NR{a[$2".gpg"];next}(($5 in a) && ($0~/decrypted/))' filea fileb



回答2:


Create a script named compare.awk. Paste this inside:

FILENAME=="fileB" && $5 ~ /xml/ {
     if ($6 == "decrypted" && $7 == "successfully.") {
         decrypted_file[$5] = 1;
     } else {
         decrypted_file[$5] = 2;
     }
}
FILENAME=="fileA" && $2 ~ /xml/ {
    if (decrypted_file[$2".gpg"] == 1) {
        print $2" exist and decrypted";
    } else if (decrypted_file[$2".gpg"] == 2) {
        print $2" exist but not decrypted";
    } else {
        print $2" not exist in fileB";
    }
}

Call it by:

awk -F' ' -f compare.awk fileB fileA

[EDIT] For shell without awk script, (still need grep, sed, cut and wc tho):

#!/bin/bash

TESTA=`grep ".xml" fileA | cut -d' ' -f2`
TESTB=`grep ".xml" fileB | cut -d' ' -f5,6,7 | sed 's/ /-/g'`
DECRYPT_YES=""
DECRYPT_NO=""

for B in ${TESTB}
do
 DECRYPT_B=`echo ${B} | sed 's/.*gpg-decrypted-successfully\./1/'`
 if [ ${DECRYPT_B} == "1" ]
 then
   DECRYPT_YES=${DECRYPT_YES}" "`echo ${B} | sed 's/\.gpg.*//g'`
 else
   DECRYPT_NO=${DECRYPT_NO}" "`echo ${B} | sed 's/\.gpg.*//g'`
 fi
done

for FILE_A in ${TESTA}
do
 if [ `echo ${DECRYPT_YES} | grep "${FILE_A}" | wc -l` == 1 ]
 then
  echo ${FILE_A}" exist and decrypted"
 elif [ `echo ${DECRYPT_NO} | grep "${FILE_A}" | wc -l` == 1 ]
 then
  echo ${FILE_A}" exist but not decrypted"
 else
  echo ${FILE_A}" not exist"
 fi
done



回答3:


Here's a script:

#!/bin/sh

FILEA=fileA
FILEB=fileB

awk -F" " ' { print $2 } ' $FILEA > .tmpfileA 
awk -F" " ' { print $5 } ' $FILEB | sed 's/\.gpg//' | grep 'decrypted successfully' > .tmpfileB


diff .tmpfileA .tmpfileB

rm -f .tmpfileA
rm -f .tmpfileB

All you'll need to change is the variables FILEA and FILEB

When executing it with the inputs you provided it gives the following result:

$ testAB.ksh 
2d1
< paybackFile_521000-845442_000_20120701.xml
$ 


来源:https://stackoverflow.com/questions/12257976/i-have-two-files-how-should-i-compare-these-files-using-shell-and-awk-script

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!