Trying to find the distinct lines using uniq command

Platform :Oracle Linux 6.4
Shell : bash

The below file has 7 lines , some of them are duplicates. There are only 3 distinct lines. But why is the uniq command still showing 7 ?
I just want the distinct lines to be returned.

$ cat test.txt
SELECT FC.COORD_SET_ID FROM OM_ORDER_FLOW F,  - UPDATE
SELECT NVL(:B5 , F.PROCESS_STATUS_ID),  - UPDATE
SELECT FC.COORD_SET_ID FROM OM_ORDER_FLOW F,  - UPDATE
select * from om_jms_event  - UPDATE
SELECT NVL(:B5 , F.PROCESS_STATUS_ID),  - UPDATE
select * from om_jms_event  - UPDATE
SELECT FC.COORD_SET_ID FROM OM_ORDER_FLOW F,  - UPDATE
$
$ wc -l test.txt
7 test.txt
$ grep -c UPDATE test.txt | uniq
7
$   
$ grep -c UPDATE test.txt | sort | uniq
7
$
sort test.txt | uniq | grep -c UPDATE
1 Like

That's because uniq expects its input to be sorted...so that duplicate lines are adjacent to each other...in fact uniq can be eliminated by using the "-u" option to sort...

sort -u test.txt | grep -c UPDATE
1 Like