Linux-admin-uniq-command

提供:Dev Guides
移動先:案内検索

Linux Admin-uniqコマンド

以下は、 uniq で使用される一般的なスイッチです。 このコマンドは、繰り返される行を報告または省略します。

Switch Action
-c Prefix lines by the number of occurrence
-i Ignore case
-u Only print unique lines
-w Check chars, compare no more than n chars
-s Skip chars, avoid comparing the first two N characters
-f Skip fields, avoid comparing first N fields
-D Print all duplicate line groups

以前のいくつかの例でuniqを簡単に使用しました。 uniq コマンドを使用すると、一致に基づいてファイルの行をフィルタリングできます。 たとえば、営業部門のMatt Davisという名前の2人目の従業員がいるとします。 3日後、会計部門は次の四半期の販売参加賞の新しい見積もりを必要とします。 次のコマンドを使用して、従業員リストを確認できます。

[root@centosLocal centos]# cat ./Documents/names.txt | wc -l
30
[root@centosLocal centos]#

年次参加賞の30人を販売部門に伝えます。 会計が矛盾に気付く可能性は十分にあるかもしれません:彼らは生産された29個のユニークな賞プラークだけを必要としました。 もう一度試してみましょう-

[root@centosLocal Documents]# cut -d ":" -f 1,2 ./names.txt | sort | uniq | wc -l
29
[root@centosLocal Documents]#

これで、セールス部門に正確な数のユニークな参加賞をアカウンティングに提供するのに十分な情報が得られました(2つのユニークなプラークを作成するために支払う必要はありません)。 「Matt Davis」の場合は1秒だけを複製します)。

-一意の行を探す場合、常にsortを使用して、出力をuniqにパイピングします。 非UNIQエントリがインラインシーケンスでない場合、重複行とは見なされません。

すぐにレポートを生成して、オフィスを共有する営業員の数をお知らせします-

[root@centosLocal Documents]# sort -t":" -k3 ./names.txt | cut -d ":" -f3  |
uniq -c | sort -n
  1 100
  1 108
  1 201
  1 203
  1 204
  1 205
  1 206
  1 301
  1 304
  1 404
  1 405
  1 501
  1 504
  1 602
  1 603
  1 608
  1 702
  1 902
  2 101
  2 102
  2 305
  2 901
  2 903
  3 403

[root@centosLocal Documents]#