我有两个文件,每个文件都有一个文件名的md5校验和。两者都在单独的文件夹中。粘贴这些文件时,我正在寻找一种执行以下操作的机制:
如果$ column 3与$ column 6相匹配,则只需并排打印出这两个:
filename1 = md5_checksum filename2 = md5_checksum
filename3 = md5_checksum filename4 = md5_checksum
filename5 = md5_checksum filename6 = md5_checksum
希望的结果:
filename1 = md5_checksum filename6 = md5_checksum
因此,想象(或测试)以下输出:
md5 directoryA/* > checkA ; md5 directoryB/* > checkB
paste checkA checkB
我想说:“查找checkA,filename1也位于checkB中,尽管名称不同”(相同的校验和)
仅供参考,我尝试过:
awk > SIMILAR 'NR==FNR{ _[$4]=$4 next}{print $0, _[$4,$4] }' checkA checkB
($ 4是文件checkA和checkB上的字段)
我认为这是对我正在尝试做的最好的解释。真诚的感谢您如此迅速的回答:
# touch A/{fee,fie,foo,fum}
# touch B/{Bee,Bie,Boo,Bum}
# md5 B/* > checkB
# md5 A/* > checkA
# more checkA
MD5 (A/fee) = 2737b49252e2a4c0fe4c342e92b13285
MD5 (A/fie) = df8b712c4fe20a0df933819665770165
MD5 (A/foo) = 51ca4befb7cb5bd22766a33c73ffca5b
MD5 (A/fum) = a80b2c31cfc269e4aa2f48658b5349d9
# more checkB
# md5 B/*
MD5 (B/Bee) = b026324c6904b2a9cb4b88d6d61c81d1
MD5 (B/Bie) = 2737b49252e2a4c0fe4c342e92b13285
MD5 (B/Boo) = df8b712c4fe20a0df933819665770165
MD5 (B/Bum) = 51ca4befb7cb5bd22766a33c73ffca5b
如果我们在这里看到,则A(A / foo)中的文件foo与B / Bum类似
我希望输出是这样的:
A/foo B/Bum = 51ca4befb7cb5bd22766a33c73ffca5b
A/fee B/Bie = 2737b49252e2a4c0fe4c342e92b13285
最佳答案
基于以下内容:
如果您有两个带有文件名和校验和值的文件,则可以尝试如下操作:
awk -F'=' 'NR==FNR{a[$2]=$1;next} $2 in a{print a[$2],$1,FS,$2}' checkA checkB
测试:
$ cat checkA
MD5 (A/fee) = 2737b49252e2a4c0fe4c342e92b13285
MD5 (A/fie) = df8b712c4fe20a0df933819665770165
MD5 (A/foo) = 51ca4befb7cb5bd22766a33c73ffca5b
MD5 (A/fum) = a80b2c31cfc269e4aa2f48658b5349d9
$ cat checkB
MD5 (B/Bee) = b026324c6904b2a9cb4b88d6d61c81d1
MD5 (B/Bie) = 2737b49252e2a4c0fe4c342e92b13285
MD5 (B/Boo) = df8b712c4fe20a0df933819665770165
MD5 (B/Bum) = 51ca4befb7cb5bd22766a33c73ffca5b
$ awk -F'=' 'NR==FNR {a[$2]=$1; next} $2 in a { print a[$2], $1, FS, $2}' checkA checkB
MD5 (A/fee) MD5 (B/Bie) = 2737b49252e2a4c0fe4c342e92b13285
MD5 (A/fie) MD5 (B/Boo) = df8b712c4fe20a0df933819665770165
MD5 (A/foo) MD5 (B/Bum) = 51ca4befb7cb5bd22766a33c73ffca5b
更新:
您可以使用
gawk
函数使用gensub
获得所需的输出。$ gawk -F'=' 'NR==FNR {a[$2]=$1; next} $2 in a {print a[$2]=gensub(/.*\(([^)]+)\)/,"\\1","G",a[$2]), $1=gensub(/.*\(([^)]+)\)/,"\\1","G",$1), FS, $2}' checkA checkB
A/fee B/Bie = 2737b49252e2a4c0fe4c342e92b13285
A/fie B/Boo = df8b712c4fe20a0df933819665770165
A/foo B/Bum = 51ca4befb7cb5bd22766a33c73ffca5b