1

I need help , i have a file output.txt

ttl 128
ttl 128
ttl 128
ttl 128
ttl 1
ttl 128
ttl 255
ttl 1
ttl 64
ttl 128
ttl 128
ttl 1

i need count how many times appear the same value in the lines of the file. The final result must be something like this:

ttl 128 - 7 times
ttl 64 - 1 time
ttl 255 - 1 time
ttl 1 - 3 times

I hope you can help me. I'm trying to use grep command.

Thanks a lot

4

3 に答える 3

4

あなたはそれをuniqとで行うことができますsort

<output.txt sort -V | uniq -c

出力:

3 ttl 1
1 ttl 64
7 ttl 128
1 ttl 255
于 2013-01-14T14:14:59.377 に答える
3

このジョブには sort 、 uniq で十分です。ただし、問題の説明と同じ出力形式を取得するには、この awk 行を試してください

awk '{a[$0]++}END{for(x in a){t=a[x]>1?"times":"time";print x " - "a[x],t}} file

例えば

  • コネクタ「 - 」
  • count >1 の場合は を表示timesし、それ以外の場合timeは s を表示しません。

出力は次のとおりです。

ttl 1 - 3  times
ttl 64 - 1 time
ttl 128 - 7  times
ttl 255 - 1 time

:)

于 2013-01-14T14:52:31.420 に答える
0

You can use the sort command to sort your file and uniq to make it unique and count the occurrences:

 sort output.txt | uniq -c

Note: Only 'problem' is that the counts are in front of each occurrence and in your example after it.

于 2013-01-14T14:15:42.307 に答える