deals my
deals my

Reputation: 133

Stop awk processing when duplicate is found

I have a file that has duplicates. Intention is to check if there are any duplicates ... and stop processing incase there is a duplicate, so even if one duplicate record exists, we want to just exist awk processing. How an we do that ? The following code completely processes.

Thank you.

awk -F'|' '{same[$1]++} END  {if (same[$1]) 
               { print "Found duplicate"; }
            else
               { print "No Duplicate Found" } 
           }' pipe.txt```


Pipe.txt

Jim Smith|123 Any Street|Boston|US|02134
Jane Lee|248 Another St.|Boston|US|02130
Jim Smith|123 Any Street|Boston|UK|02134
Bret Lee|248 Another St.|Boston|US|02130
Bret Lee|123 Any Street|Boston|US|021340
Shen Lee|123 Any Street|Boston|US|021341

Upvotes: 1

Views: 58

Answers (1)

Ed Morton
Ed Morton

Reputation: 203522

$ awk -F'|' 'seen[$1]++{f=1; exit} END{print (f ? "" : "No ") "duplicate found"; exit f}' file
duplicate found
$ echo $?
1

$ seq 5 | awk -F'|' 'seen[$1]++{f=1; exit} END{print (f ? "" : "No ") "duplicate found"; exit f}'
No duplicate found
$ echo $?
0

Upvotes: 3

Related Questions