0

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 :

2
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
2
  • @thanasisp Updated
    – Emma Vaze
    Oct 13 '20 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
    Oct 14 '20 at 7:15
0
awk '/United States/ {i++} END {print i}' RS=",|\n" Files/*.csv

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

2
  • 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
    Oct 13 '20 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
    Oct 13 '20 at 11:29
1

awk

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

or

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

grep

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
0

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
5
0

Command

    #/bin/bash

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.