Linux Shell learning: uniq command usage method introduction

  
                  

The role of the uniq command: display only the lines, only for the consecutive lines that are repeated!


The following is a practical example:


[root@stu100 ~]# cat test

boy took bat home

Boy took bat home

girl took bat home

dog brought hat home

dog brought hat home

dog brought hat home


Look at the contents of the test file, you can see the continuous repeating lines


[root@stu100 ~]# uniq test

boy took bat home

girl took bat home

dog brought hat home


uniq command without any parameters, only shows consecutive repeated lines


[root@ Stu100 ~]# uniq -c test

2 boy took bat home

1 girl took bat home

3 dog brought hat home


The -c parameter displays the number of consecutive occurrences of each line in the file.


[root@stu100 ~]# uniq -d test

boy took bat home

dog brought hat home


- The d option only displays rows that appear repeatedly in the file.


[root@stu100 ~]# uniq -u test

girl took bat home


-u option shows no consecutive lines in the file .


[root@stu100 ~]# uniq -f 2 -s 2 test

boy took bat home


Ignore the first 2 lines of each line Field, ignoring the second white space and the first character of the third field, the result at home


[root@stu100 ~]# uniq -f 1 test

boy took bat Home

dog brought hat home


Ignore the first field of each line, so that the lines beginning with boy and girl seem to be consecutive lines.

Copyright © Windows knowledge All Rights Reserved