justaguy
justaguy

Reputation: 3022

awk to display differences in 2 files

I am trying to display the count and differences (if any) between two files using awk. The awk below will display the unique count of $3 in file2, but how do I also display the ids that were not found? Thank you :).

file1

ACTA2
ACTC1
APC
APOB
BRCA1
BRCA2

file2 (ACTA2, ACTC1, APC are all unique so they are used in the count)

chr10:90694965-90695138 ACTA2-1269|gc=52.6 639.7
chr10:90697803-90698014 ACTA2-1270|gc=50.2 347.6
chr15:35082598-35082771 ACTC1-254|gc=50.3 603.8
chr15:35085431-35085785 ACTC1-258|gc=54.8 633.8
chr15:35086866-35087046 ACTC1-259|gc=67.2 291.0
chr5:112043405-112043589 APC-1396|gc=70.1 334.8
chr5:112090578-112090732 APC-1397|gc=39.6 171.6
chr5:112102006-112102125 APC-1398|gc=33.6 52.3
chr5:112102876-112103097 APC-1399|gc=41.2 177.4

awk

awk -F '[- ]' '!seen[$3]++ {n++} END {print n " ids found)}' file2    

desired result (comes from file2 - already works)

3 ids found and APOB,BRCA1,BRCA2 missing

Upvotes: 0

Views: 46

Answers (2)

karakfa
karakfa

Reputation: 67557

here is a prototype

$ awk -F '[- ]' 'NR==FNR{a[$0];next} 
               ($3 in a){delete a[$3]} 
                    END {for(k in a) printf "%s ",k; print "missing"}' file{1,2}

BRCA1 BRCA2 APOB missing

with right output formatting

$ awk -F '[- ]' 'NR==FNR{a[$0];next} 
               ($3 in a){delete a[$3]; c++} 
                     END{printf "%s ids found and ", c; 
                         for(k in a) {printf "%s",sep k; sep=","} 
                         print " missing"}' file{1,2}

3 ids found and BRCA1,BRCA2,APOB missing

Upvotes: 1

Tom Fenech
Tom Fenech

Reputation: 74695

This gets you pretty close to your desired output:

$ awk -F'[ -]' 'NR == FNR { seen[$0]; next } !seen[$3]++ { n++ }
END { print n " ids found"; for (i in seen) if (!seen[i]) print i " missing" }' file1 file2
3 ids found
APOB missing
BRCA1 missing
BRCA2 missing

It basically loops through the seen array and checks the value. If it hasn't been seen in the second file !seen[i] is true.

Upvotes: 1

Related Questions