Search code examples
awkcase-insensitivecounting

Using awk, ignore casesensitve pattern when summarize lines based on the same pattern


Using awk, I would like to ignore case sensitve pattern when summarize lines based on the same pattern.

I have the following line (big thanks to Andrey (https://stackoverflow.com/users/3476320/andrey)

awk '{n=$1;$1="";a[$0]+=n}END{for(i in a){print a[i], i}}' testing.txt

The file contents:

1 Used cars
12 Drivers
1 used cars
1 used  cars
14 drivers
2 Used Cars

the actual output is

2  Used Cars
14  drivers
12  Drivers
2  used cars
1  Used cars

What I need to have:

26 drivers/Drivers (doesn't matter)
5 used cars/Used Cars (doesn't matter)

Thank you!


Solution

  • maybe the easiest way:

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