萬盛學電腦網

 萬盛學電腦網 >> Linux教程 >> Linux Shell學習:uniq命令使用方法介紹

Linux Shell學習:uniq命令使用方法介紹

class="20842">

 uniq命令的作用:顯示唯一的行,對於那些連續重復的行只顯示一次!

  接下來通過實踐實例說明:

  [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

  看test文件的內容,可以看到其中的連續重復行

  [root@stu100 ~]# uniq test

  boy took bat home

  girl took bat home

  dog brought hat home

  uniq命令不加任何參數,僅顯示連續重復的行一次

  [root@stu100 ~]# uniq -c test

  2 boy took bat home

  1 girl took bat home

  3 dog brought hat home

  -c 參數顯示文件中每行連續出現的次數。

  [root@stu100 ~]# uniq -d test

  boy took bat home

  dog brought hat home

  -d選項僅顯示文件中連續重復出現的行。

  [root@stu100 ~]# uniq -u test

  girl took bat home

  -u選項顯示文件中沒有連續出現的行。

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

  boy took bat home

  忽略每行的前2個字段,忽略第二個空白字符和第三個字段的首字符,結果at home

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

  boy took bat home

  dog brought hat home

  忽略每行的第一個字段,這樣boy ,girl開頭的行看起來是連續重復的行。

copyright © 萬盛學電腦網 all rights reserved