shell 比较两个具有各自字段的文件,并以特定格式输出

vc9ivgsu  于 2022-11-16  发布在  Shell
关注(0)|答案(2)|浏览(194)

我正在比较两个文件
food1.txt文件并比较food2.txt文件,如下所示
第一个

  • 我的工作。
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
  • 输出..但我不喜欢它
-----[ WARN ] : pizza=1ea
-----[  OK  ] : chicken=5ea
-----[  OK  ] : tooboo=4ea
-----[ WARN ] : orange=2ea

我想一起打印比较目标

  • 我想要输出,像这样
food2.txt                   food1.txt
pizza=2ea   : [ NotMatch ] : pizza=1ea
            : [    OK    ] : chicken=5ea
            : [    OK    ] : tooboo=4ea
            : [ NotExist ] : orange=2ea
grape=3ea   : [ NotExist ] :
melon=1ea   : [ NotExist ] :

这可能吗?请帮帮我。

ovfsdjhp

ovfsdjhp1#

使用您展示的示例,请尝试以下awk程序。用GNU awk编写和测试。

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

对于您所示的样品,***输出***如下:

food1.txt                   food2.txt
pizza=1ea  :[ NotMatch  ] : pizza=2ea
           :[    OK     ] : chicken=5ea
grape=3ea  :[ NotExist  ] :
           :[    OK     ] : tooboo=4ea
melon=1ea  :[ NotExist  ] :
           :[ NotExist  ] : orange=2ea

***说明:***添加上述代码的详细说明。

awk '                                                        ##Starting awk program from here.
BEGIN{                                                       ##Starting BEGIN section from here.
  FS="="                                                     ##Setting field separator as = 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]=$2                                                ##Creating array named arr1 with index of 1st field and value is $2.
  next                                                       ##next will skip all further statements from here.
}
($1 in arr1){                                                ##Checking condition if $1 is present in arr1 then do following.
  if($2==arr1[$1]){                                          ##Checking condition if 2nd field is equal to arr1 value.
     print  "           :[    OK     ] : " $0                ##Printing ok message with current line of food2.txt here.
  }
  else if($2!=arr1[$1]){                                     ##Else(in case 2nd field is NOT equal to arr1 value) then do following.
     print $1 FS 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.
     }
  }
}
' food1.txt food2.txt                                        ##Mentioning Input_file names here.
cbjzeqam

cbjzeqam2#

由于bash被标记,这里有一个使用关联数组的解决方案。第一个while循环阅读第一个文件填充数组。第二个while循环迭代第二个文件,检查键和/或值是否与数组中的条目匹配,并删除它们。最后,for循环迭代数组中剩余的“未检查”条目。
第一个

相关问题