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

后端 未结 3 1036
清酒与你
清酒与你 2021-01-27 13:17

I have two files

Content of file A

paybackFile_537214-760887_000_20120801.xml
paybackFile_354472-544899_000_20120801.xml
paybackFile_62-11033_000_2012080         


        
相关标签:
3条回答
  • 2021-01-27 13:21

    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
    $ 
    
    0 讨论(0)
  • 2021-01-27 13:28
    awk 'FNR==NR{a[$2".gpg"];next}(($5 in a) && ($0~/decrypted/))' filea fileb
    
    0 讨论(0)
  • 2021-01-27 13:30

    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
    
    0 讨论(0)
提交回复
热议问题