Let's take an example file as follows:
If you want to find unique lines of this file, it can be done as
However, it changes the order of occurrence of lines.
Following "awk" command will find unique lines of a file without sorting the file:
awk 'x[$0]++' keep on incrementing the hash array "x" of line from 0 or undefined to 1,2 and so on. Since there is no code for awk in {}, so it by default prints the line. When ! is appended in front of x[$0]++ , it will only print line when value of its hash array "x" is 0 or undefined.
$ cat file
AAAA
FFFF
BBBB
BBBB
CCCC
AAAA
FFFF
DDDD
If you want to find unique lines of this file, it can be done as
$ cat file|sort|uniq
AAAA
BBBB
CCCC
DDDD
FFFF
However, it changes the order of occurrence of lines.
Following "awk" command will find unique lines of a file without sorting the file:
$ awk '!x[$0]++' file
AAAA
FFFF
BBBB
CCCC
DDDD
awk 'x[$0]++' keep on incrementing the hash array "x" of line from 0 or undefined to 1,2 and so on. Since there is no code for awk in {}, so it by default prints the line. When ! is appended in front of x[$0]++ , it will only print line when value of its hash array "x" is 0 or undefined.
No comments:
Post a Comment