Contents
To find unique occurrences where the lines are not adjacent a file needs to be sorted before passing to uniq . uniq will operate as expected on the following file that is named authors. txt . As duplicates are adjacent uniq will return unique occurrences and send the result to standard output.
How do I get unique lines in a file?
Find unique lines
- The file must be sorted first. sort file | uniq -u will output to console for you. – …
- I think the reason sort file | uniq shows all the values 1 time is because it immediately prints the line it encounters the first time, and for the subsequent encounters, it just skips them. –
How do I get unique records in Unix?
Let us now see the different ways to find the duplicate record.
- Using sort and uniq: $ sort file | uniq -d Linux. …
- awk way of fetching duplicate lines: $ awk ‘{a[$0]++}END{for (i in a)if (a[i]>1)print i;}’ file Linux. …
- Using perl way: …
- Another perl way: …
- A shell script to fetch / find duplicate records: