i was trying to read a file and count a specific number at a specific place and show how many times it appears, for example: 1st field are numbers, 2nd field brand name, 3rd field a group they belong to, 4th and 5th not important.
1:audi:2:1990:5 2:bmw:2:1987:4 3:bugatti:3:1988:19 4.buick:4:2000:12 5:dodge:2:1999:4 6:ferrari:2:2000:4
As an output, i want to search by column 3, and group together 2's(by brand name) and count how many of them i have. The output i am looking for should look like this:
1:audi:2:1990:5 2:bmw:2:1987:4 5:dodge:2:1999:4 6:ferrari:2:2000:4
4 -> showing how many lines there are. I have tried taken this approach but can't figure it out:
file="cars.txt"; sort -t ":" -k3 $file #sorting by the 3rd field grep -c '2' cars.txt # this counts all the 2's in the file including number 2.
I hope you understand. and thank you in advance.