简体   繁体   中英

Linux shell script for counting similar words in a file

Am having a log file in following format:

201208290101
201208290101
201208290101
201208290101
201208290101
201208291222
201208291222
201208291222
201208291222
201209300242
201209300242
201209300242

i want to count the number of events occurred at some particular time so i have to count how many times some particular time stamp occurred in log file . for example output of the above would be something like :

201208290101  = 5
201208291222  = 4
201209300242  = 3

any suggestion on how to count this ?

Just pipe the file through uniq -c .

$ uniq -c
201208290101
201208290101
201208290101
201208290101
201208290101
201208291222
201208291222
201208291222
201208291222
201209300242
201209300242
201209300242
^D
      5 201208290101
      4 201208291222
      3 201209300242

您可以awk关联数组:

 awk '{a[$0]++;} END{for(i in a) print i," = ", a[i]}' filename

If everything is sorted, then

cat yourfile.txt | uniq -c

Otherwise, you need to sort it before you can use uniq:

cat yourfile.txt | sort | uniq -c

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM