Search code examples
fileunixdelimited

How to get count of word occurrence at specific location in delimited file in Unix


I have pipe delimited file, I want to check for value 'America' at 5th position (column) of each record. America word can appear in any other columns as well, so grep -o is not giving correct result.
Any other way to check the occurrence of word at specific location when file is delimited?


Solution

  • Script like this can do the work

    awk '$5="America" {print}' filename|wc -l
    

    Of course you can do it only with awk like this:

    awk 'BEGIN {i=0} $5="America" {i+=1} END {print i}' filename
    

    To use different delimiter you can use in awk this way:

    awk -F\| 'BEGIN {i=0} $5="America" {i+=1} END {print i}' filename