简体   繁体   English

awk粘贴比较打印

[英]awk paste comparison printing

I have two files, each file has an md5 checksum of a file name. 我有两个文件,每个文件都有一个文件名的md5校验和。 Both are in separate folders. 两者都在单独的文件夹中。 When I paste these files, I am looking for a mechanism to do the following: 粘贴这些文件时,我正在寻找一种执行以下操作的机制:

if $column 3 matches $column 6, solely print out those two side by side: 如果$ column 3与$ column 6相匹配,则只需并排打印出这两个:

filename1 = md5_checksum filename2 = md5_checksum
filename3 = md5_checksum filename4 = md5_checksum
filename5 = md5_checksum filename6 = md5_checksum

Hopeful result: 希望的结果:

filename1 = md5_checksum filename6 = md5_checksum

So imagine (or test) the output of: 因此,想象(或测试)以下输出:

md5 directoryA/* > checkA ; md5 directoryB/* > checkB
paste checkA checkB

I'd like to say: "Look in checkA, filename1 is also in checkB albeit a different name" (same checksum) 我想说:“查找checkA,filename1也位于checkB中,尽管名称不同”(相同的校验和)

FYI, what I have tried: 仅供参考,我已经尝试过:

awk > SIMILAR 'NR==FNR{ _[$4]=$4 next}{print $0, _[$4,$4] }' checkA checkB

($4 being the field on both files checkA and checkB) ($ 4是文件checkA和checkB上的字段)


Here is what I perceive is the best explanation as to what I'm trying to do. 我认为这是对我正在尝试做的最好的解释。 Sincerest thanks for answering so quickly: 真诚的感谢您这么快回答:

# 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

If we see here, file foo in A (A/foo) is similar to B/Bum 如果我们在这里看到,则A(A / foo)中的文件foo与B / Bum类似

I'd like the output to be something like: 我希望输出是这样的:

A/foo B/Bum = 51ca4befb7cb5bd22766a33c73ffca5b
A/fee B/Bie = 2737b49252e2a4c0fe4c342e92b13285

Based on the following: 基于以下内容:

I'd like to say: "Look in checkA, filename1 is also in checkB albeit a different name" (same checksum) 我想说:“查找checkA,filename1也位于checkB中,尽管名称不同”(相同的校验和)

If you have two files with filename and checksum values then you can try something like this: 如果您有两个带有文件名和校验和值的文件,则可以尝试如下操作:

awk -F'=' 'NR==FNR{a[$2]=$1;next} $2 in a{print a[$2],$1,FS,$2}' checkA checkB

Test: 测试:

$ 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

Update: 更新:

You can use gawk to get your desired output by using gensub function. 您可以使用gensub函数使用gawk获得所需的输出。

$ 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
join -o 1.2,2.2,1.3,1.4 -j 4 <(sort -k4,4 checkA) <(sort -k4,4 checkB)
(A/fee) (B/Bie) = 2737b49252e2a4c0fe4c342e92b13285
(A/foo) (B/Bum) = 51ca4befb7cb5bd22766a33c73ffca5b
(A/fie) (B/Boo) = df8b712c4fe20a0df933819665770165

Pipe into tr -d '()' if you need to get rid of the parentheses. 如果需要除去括号,请将其传递到tr -d '()'中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM