简体   繁体   中英

awk paste comparison printing

I have two files, each file has an md5 checksum of a file name. 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:

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)

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)


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

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)

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.

$ 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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