AWK tutorial: awk sort uniq

In daily development, we often use awk command to cut and sort log file columns and count them.

awk sort uniq count example

ylspiritdeMacBook-Pro:awk ylspirit$ cat test_4.txt  | awk '{print $1}'  | sort | uniq -c
ylspiritdeMacBook-Pro:awk ylspirit$ sort -k1 test_4.txt | awk '{a[$1]++}END{for(i in a) {print i, a[i]}}'

awk column count

ylspiritdeMacBook-Pro:awk ylspirit$ awk '{if($1=="111") {i++}}END{print i}' test_4.txt

linux Sort

  • – n : sorted by the size of the value;
  • – r : reverse order
  • -k : which interval (field) to sort
ylspiritdeMacBook-Pro:awk ylspirit$ cat test_4.txt  | awk '{print $1}'  | sort | uniq -c | sort -n

This entry was posted in Awk Command, Text Processing and tagged , , , . Bookmark the permalink.