Shell AWK用于逐行检查不同文件的第一列,并查看差异

Shell AWK用于逐行检查不同文件的第一列,并查看差异,shell,awk,command-line,Shell,Awk,Command Line,我有两个这样的文件: 文件1: fa65c95d0 ./checklist.chk 316856ab2 ./.vscode/settings.json 6075df9cc ./sha256sum.txt 36d59e45b ./md5sum.txt 文件2: fa65c95d0 ./checklist.chk 222222222 ./.vscode/settings.json 644ddd555 ./sha256sum.txt 36d59e45b ./md5sum.txt 我

我有两个这样的文件:

文件1:

fa65c95d0  ./checklist.chk
316856ab2  ./.vscode/settings.json
6075df9cc  ./sha256sum.txt
36d59e45b  ./md5sum.txt
文件2:

fa65c95d0  ./checklist.chk
222222222  ./.vscode/settings.json
644ddd555  ./sha256sum.txt
36d59e45b  ./md5sum.txt
我想逐行比较这些文件的第一列,看看File2上有什么不同

期望输出

./.vscode/settings.json is modified
./sha256sum.txt is modified
我在网上搜索并尝试使用comm,但这个脚本并不完全符合我的要求

comm -3 sum1.txt sum2.txt | awk '$1 {print $2}'
此文件的输出

comm: file 1 is not in sorted order
comm: file 2 is not in sorted order
./.vscode/settings.json
./.vscode/settings.json
./sha256sum.txt
./md5sum.txt
./sha256sum.txt
./md5sum.txt
使用
diff(1)

vimdiff(1)


请尝试以下内容

awk '
FNR==NR{
  a[$2]=$1
  next
}
($2 in a) && a[$2]!=$1{
  print $0,"is modified"
}
' Input_file1  Input_file2
解释:在此处添加上述代码的详细解释

awk '                             ##Starting awk program from here.
FNR==NR{                          ##Checking condition FNR==NR which will be TRUE when Input_file1 is being read.
  a[$2]=$1                        ##Creating an array named a whose index is $2 and value is $1for current line.
  next                            ##next will skip all statements from here.
}                                 ##Closing BLOCK for FNR==NR here.
($2 in a) && a[$2]!=$1{           ##Checking condition if 2nd field of Input_file2 is present in array a AND value of array a with index $2 is NOT equal to $1 of Input_file2 then do following.
  print $0,"is modified"          ##Printing current line the string is modified as per OP need.
}                                 ##Closing BLOCK of statements which were getting executed for Input_file2.
' Input_file1  Input_file2        ##Mentioning Input_file names here.

欢迎来到SO,on,SO,我们鼓励用户在他们提出的问题中发布他们的努力,以解决他们自己的问题,请在您的问题中添加相同的内容,然后让我们知道。@RavinderSingh13感谢欢迎抱歉没有添加尝试,我刚刚忘记了,我已经尝试了半个小时,我很困惑。@Varian,谢谢你的努力,继续努力,并在这个伟大的网站上愉快地学习,所以,干杯。如果我没有错,
diff
提供输出,如果两个文件列副行相同正确吗?否则它会给每条线带来差异。请让我知道这是否正确,仅供我理解,干杯。@RavinderSingh13:它实际上为您提供了一个有效的补丁程序,可以将第一个文件转换为第二个文件的状态
diff -u file1 file2
--- file1   2019-12-02 18:44:46.525142623 +0100
+++ file2   2019-12-02 18:44:57.253178157 +0100
@@ -1,4 +1,4 @@
 fa65c95d0  ./checklist.chk
-316856ab2  ./.vscode/settings.json
-6075df9cc  ./sha256sum.txt
+222222222  ./.vscode/settings.json
+644ddd555  ./sha256sum.txt
 36d59e45b  ./md5sum.txt
vimdiff file1 file2
awk '
FNR==NR{
  a[$2]=$1
  next
}
($2 in a) && a[$2]!=$1{
  print $0,"is modified"
}
' Input_file1  Input_file2
awk '                             ##Starting awk program from here.
FNR==NR{                          ##Checking condition FNR==NR which will be TRUE when Input_file1 is being read.
  a[$2]=$1                        ##Creating an array named a whose index is $2 and value is $1for current line.
  next                            ##next will skip all statements from here.
}                                 ##Closing BLOCK for FNR==NR here.
($2 in a) && a[$2]!=$1{           ##Checking condition if 2nd field of Input_file2 is present in array a AND value of array a with index $2 is NOT equal to $1 of Input_file2 then do following.
  print $0,"is modified"          ##Printing current line the string is modified as per OP need.
}                                 ##Closing BLOCK of statements which were getting executed for Input_file2.
' Input_file1  Input_file2        ##Mentioning Input_file names here.