I'm trying to match the word United States from a folder containing 5 csv files, However, the command I'm using checks for single occurrence in the line. I also want to sum other two columns based on the matched string.

awk -F "," '{if (/United States/) sum_1 += $2;sum_2 += $3} END {print sum_1, sum_2}' Files/*.csv

Yes I'm trying to check more than one occurrences in the line with awk.

So if the line has two occurrences of United States it should give :

awk '/United States/{x++;}END{print x}' Files/*csv

Example file

United States,1,2,3
Donald TRump,1,2,3
United States,1,2,3,United States
  • @thanasisp Updated
    – Emma Vaze
    Commented Oct 13, 2020 at 4:19
  • What about this first line of code you showed us, where you want to sum the second and third column values? It doesn't seem to play any role in the accepted answer anymore.
    – AdminBee
    Commented Oct 14, 2020 at 7:15

4 Answers 4

awk '/United States/ {i++} END {print i}' RS=",|\n" Files/*.csv

RS - The input record separator, by default a newline.

  • I tried this,and I get the count as 3500 but with grep -o "United States" *.csv | wc -l I get 3502. Why the difference in awk?
    – Emma Vaze
    Commented Oct 13, 2020 at 8:59
  • The discrepancy between your grep -o <pattern> *.csv | wc -l output and the awk-based solution above could come from the fact that occurrences of the sought pattern may have neither a comma nor a CR to separate them, just any other character including space and tab. If so, my solution covers those cases.
    – Cbhihe
    Commented Oct 13, 2020 at 11:29


You can use the string function gsub, which returns the number of substitutions made per line. This is a convenient way to count occurences per line, and you do it for all lines, so you count all of them.

awk '{cnt += gsub(/United States/,"&")} END {print cnt}' *.csv

If you want to use this check inside an awk script, like in your example, you can have a syntax like:

awk '{ if (gsub(/pattern/"&")) {"do stuff here"} }' file


awk '{x = gsub(/pattern/"&"); "do stuff with x here"; }' file


Also using grep, with -o we will get one line per match in the output, and the count of the output lines is the result.

grep -o "United States" *.csv | wc -l

Given the sample input file you provided, @nezabudka's answer:

awk 'BEGIN {RS=",|\n"} /United States/ {i++} END {print i}' inputFile

seems correct. This prompts me to ask you: are you certain that your sample input file is representative of all your field-separators ? If not, here is an alternate solution which admits input file cases such as:

> cat inputFile
United States,1,2,3
Donald TRump,1,2,3 United States blih blah \!?# bluh  United States
United States,1,2,3,United    States

> awk 'BEGIN {RS=",|\n| "} 
       /United/ {rec=NR; next} 
       /States/ && NR==rec+1 {i++} 
       END {print i}'  inputFile



    awk '{print gsub("United States",$0)}' filename| awk 'BEGIN{sum=0}{sum=sum+$1}END{print sum}'

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .