0

I have a single column CSV file with no header and I want to iteratively find the value of each row and count the number of times it appears in several files.

Something like this:

for i in file.csv:
 zcat *json.gz | grep i | wc -l

However, I don't know how to iterate through the csv and pass the values forward

Imagine that file.csv is:

foo,
bar

If foo exists 20 times in *json.gz and bar exists 30 times in *json.gz, I would expect the output of my command to be:

20
30

Here is the solution I found:

while IFS=',' read -r column; do
  count=$(zgrep -o "$column" *json.gz | wc -l)
  echo "$column,$count"; done < file.csv
Wiseface
  • 183
  • 2
  • 11

2 Answers2

0

You can achieve that with a single grep operation treating file.csv as a patterns file (obtaining patterns one per line):

grep -f file.csv -oh *.json | wc -l

  • -o - to print only matched parts
  • -h - to suppress file names from the output
RomanPerekhrest
  • 88,541
  • 4
  • 65
  • 105
  • This gave me one number, which I assume is sum of each row count. What I would like is the total number of times that each row value in files.csv is found in *.json. So if there are 20 rows in files.csv, I want 20 counts, based on how many times each row value appears in *.json. – Wiseface Jan 26 '23 at 12:18
-1

You can iterate through output of cat run through subprocess:

for i in `cat file.csv`: # iterates through all the rows in file.csv
   do echo "My value is $i"; done;