Advertisement
Guest User

Untitled

a guest
Feb 21st, 2017
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.66 KB | None | 0 0
  1. # In a CSV file, how to count all lines matching a condition?
  2.  
  3. Following on our CSV file processing series, here is how to count the number of lines that match a condition.
  4.  
  5. We’ll keep using `awk`. Here is the command to run:
  6.  
  7. ```bash
  8. awk -F, '{if ($5 == "foo") count+=1} END {print count}'
  9. ```
  10.  
  11. Easy enough, `-F,` tells each line use the comma to delimit each column.
  12. Then for each line in the file, check if the 5th column is equal to `foo`. If true then increment the `count` variable by 1.
  13.  
  14. When done processing all lines, just print `count`.
  15.  
  16. Voila, you’re ready to process GB of CSV files without killing your spreadsheet software or writing a single line of code!
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement