Home > Back-end >  Compare two files with string and output required for specific format
Compare two files with string and output required for specific format

Time:02-12

Thank you for your answer at https://stackoverflow.com/questions/71080087 @RavinderSingh13

In questions/71080087, I compared two files by the field delimiter.

one more question... I want to ask a more detailed question

This is a list of file systems provided by each of the two servers.

cat volume1.txt (by server1)
/         80G    xfs  /dev/mapper/rootvg-lv_root 
/boot     1014M  xfs  /dev/sda2 
/boot/efi 500M   vfat /dev/sda1
/swlogs   10G    xfs  /dev/mapper/datavg-lv_swlogs
# cat volume2.txt (by server2)
/         33G    xfs  /dev/mapper/rhel-root 
/boot/efi 599M   vfat /dev/sda1 
/boot     1014M  xfs  /dev/sda2 

Is it possible to output as below? (order of the lines doesn't matter)

volume1.txt                                                        volume2.txt
/       80G    xfs  /dev/mapper/rootvg-lv_root     : [ NotMatch ] : /       33G    xfs  /dev/mapper/rhel-root 
/boot     1014M  xfs  /dev/sda2                    : [    OK    ] : 
/boot/efi 500M   vfat /dev/sda1                    : [ NotMatch ] : /boot/efi 599M   vfat /dev/sda1
/swlogs   10G    xfs  /dev/mapper/datavg-lv_swlogs : [ NotExist ] : 

my work...

FOOD1=`cat ./food1.txt`
FOOD2=`cat ./food2.txt`

echo "$FOOD1" | while read ACCOUNT
do
grep -w $ACCOUNT ./food2.txt  >/dev/null 2>&1
if [ $? -eq 0 ] ; then
echo "-----[  OK  ] : $ACCOUNT"
else
echo "-----[ WARN ] : $ACCOUNT"
fi
done 

If you don't understand my question please tell me.

CodePudding user response:

With your shown samples, please try following awk code. Written and tested in GNU awk.

awk '
BEGIN{
  print ARGV[1]"                   "ARGV[2]
}
FNR==NR{
  arr1[$1]=$0
  next
}
($1 in arr1){
  if($0==arr1[$1]){
     print  $0 "           :[    OK     ] : "
  }
  else if($0!=arr1[$1]){
     print arr1[$1]"  :[ NotMatch  ] : "$0
  }
  arr2[$1]
  next
}
{
  print $0"  :[ NotExist  ] : "
}
END{
  for(i in arr1){
     if(!(i in arr2)){
        print arr1[i]"           :[ NotExist  ] : "
     }
  }
}
' volume1.txt  volume2.txt

Explanation: Adding detailed explanation for above.

awk '                                                        ##Starting awk program from here.
BEGIN{                                                       ##Starting BEGIN section from here.
  print ARGV[1]"                   "ARGV[2]                  ##Printing passed Input_file names here.
}
FNR==NR{                                                     ##Checking condition FNR==NR which will be TRUE when food1.txt is being read.
  arr1[$1]=$0                                                ##Creating array named arr1 with index of 1st field and value is $0.
  next                                                       ##next will skip all further statements from here.
}
($1 in arr1){                                                ##Checking condition if $1 is present in arr1 then do following.
  if($0==arr1[$1]){                                          ##Checking condition if whole line is equal to arr1 value.
     print  "           :[    OK     ] : " $0                ##Printing ok message with current line of food2.txt here.
  }
  else if($0!=arr1[$1]){                                     ##Else(in case whole line is NOT equal to arr1 value) then do following.
     print arr1[$1]"  :[ NotMatch  ] : "$0             ##Printing first field FS value of arr1 followed by NotMatch followed by current line from food2.txt.
  }
  arr2[$1]                                                   ##Making an entry of current $1 for arr2 array here.
  next                                                       ##next will skip all further statements from here.
}
{
  print $0"  :[ NotExist  ] : "                              ##printing current line followed by NotExist statement.
}
END{                                                         ##Starting END block for this program from here.
  for(i in arr1){                                            ##Traversing through arr1 elements here.
     if(!(i in arr2)){                                       ##Checking condition if key i is NOT present in arr2 then do following.
        print "           :[ NotExist  ] : "i FS arr1[i]     ##printing NOtExist statements followed by i FS and arr1 value.
     }
  }
}
' volume1.txt  volume2.txt                                   ##Mentioning Input_file names here.
  • Related