robert patrik
robert patrik

Reputation: 89

Comparing two files script and finding the unmatched data

I am having two .txt files with data stored in the format

1.txt

ASF001-AS-ST73U12
ASF001-AS-ST92U14
ASF001-AS-ST105U33
ASF001-AS-ST107U20

and

2.txt

ASF001-AS-ST121U21
ASF001-AS-ST130U14
ASF001-AS-ST73U12
ASF001-AS-ST92U14

` I need to find the files which are in 1.txt but not in 2.txt.

I tried to use

diff -a --suppress-common-lines -y 1.txt 2.txt > finaloutput

but it didn't work

Upvotes: 1

Views: 708

Answers (2)

David C. Rankin
David C. Rankin

Reputation: 84561

A relatively simple bash script can do what you need:

#!/bin/bash

while read line || test -n "$line"; do 
    grep -q $line "$2" || echo "$line"
done < "$1"

exit 0

output:

$ ./uniquef12.sh dat/1.txt dat/2.txt
ASF001-AS-ST105U33
ASF001-AS-ST107U20

Upvotes: 0

anubhava
anubhava

Reputation: 785156

Rather than diff you can use comm here:

comm -23 <(sort 1.txt) <(sort 2.txt)
ASF001-AS-ST105U33
ASF001-AS-ST107U20

Or this awk will also work:

awk 'FNR==NR {a[$1];next} $1 in a{delete a[$1]} END {for (i in a) print i}' 1.txt 2.txt
ASF001-AS-ST107U20
ASF001-AS-ST105U33

Upvotes: 1

Related Questions