Shell 用于查找两个文件之间出现次数增加的脚本

Shell script to find increase in occurance count between two files

File1.log

2000 apple
2333 cat
5343 dog
1500 lion

File2.log

2500 apple
2333 cat
1700 lion

需要shell脚本输出如下:

500 apple
200 lion

已经尝试了很多解决方案,但没有任何解决方案,因为我同时拥有文本和字符串。有人可以帮忙吗?谢谢

编辑(由 RavinderSingh13 撰写):添加了 OP 在 post 评论中显示的 OP 的努力:

#!/bin/bash
input1="./File1.log"
input2="./File2.log"
while IFS= read -r line2
do
  while IFS=read -r line1
  do
     echo "$line1"
  done < "$input1"
  echo "$line2"
done < "$input2"
awk '{if (!( in entry)) { entry[]= } else { delta=-entry[]; if (delta!=0) {print delta,} } }' FILE_1 FILE2

您也可以将其放入文件中,例如delta.awk:

{
  if (!( in entry)) {
    entry[]= 
  } else { 
    delta=-entry[]
    if (delta !=0) { # Only output lines of non-zero increment/decrement
      print delta,
    } 
  } 
}

通过awk -f delta.awk FILE_1.txt FILE_2.txt调用。

能否请您尝试以下。

awk 'FNR==NR{a[]=;next}  in a && (-a[])>0{print -a[],}' file1 file2

添加上述解决方案的非线性形式:

awk '
FNR==NR{
  a[]=
  next
}
( in a) && (-a[])>0{
  print -a[],
}
'  Input_file1  Input_file2

说明:在此处添加对上述解决方案的详细说明。

awk '                             ##Starting awk program from here.
FNR==NR{                          ##Checking condition if FNR==NR which will be TRUE once file1 is being read then do following.
  a[]=                        ##Creating an array a whose index is  and value is  of current line.
  next                            ##Using next function of awk, to skip all further lines from here.
}                                 ##Closing condition BLOCK for FNR==NR here.
( in a) && (-a[])>0{        ##Checking condition if  is present in array a AND difference of  and array a with index  is greater than 0 then do following.
  print -a[],               ##Printing difference between  and array a with index  along with current  here.
}                                 ##Closing BLOCK for above condition here.
' file1 file2                     ##Mentioning Input_file names here.