uniq -c is not working with awk? uniq -c is not working with awk? shell shell

uniq -c is not working with awk?


You're running the command on a single line, so it makes sense that you're getting 1.

Having the following test.txt:

Mar 22 11:20:34 DHCP-IP dhcp: DHCPREQUEST for 10.1.1.1 from aa:00:00:00:00:00 (pc) via 10.1.1.2Mar 22 11:20:35 DHCP-IP dhcp: DHCPREQUEST for 10.1.1.1 from aa:00:00:00:00:00 (pc) via 10.1.1.2Mar 22 11:20:36 DHCP-IP dhcp: DHCPREQUEST for 10.1.1.1 from aa:00:00:00:00:00 (pc) via 10.1.1.2

The command:

grep DHCPREQUEST test | awk '{print $8}' | uniq -c

prints:

3 10.1.1.1


Following single awk may help you on same too.

awk 'match($0,/[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+/){array[substr($0,RSTART,RLENGTH)]++} END{for(i in array){print array[i],i}}'  Input_file

Adding a non-one liner form of solution too now.

awk 'match($0,/[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+/){  array[substr($0,RSTART,RLENGTH)]++  }END{  for(i in array)                         {  print array[i],i                    }}'   Input_file