- 此问题在此处已有答案**:
Count all occurrences of a string in lots of files with grep(16个答案)
3天前关闭。
这篇文章是编辑和提交审查3天前。
我有一个没有标题的单列CSV文件,我想迭代地找到每一行的值,并计算它在几个文件中出现的次数。
大概是这样的
for i in file.csv:
zcat *json.gz | grep i | wc -l
但是,我不知道如何迭代csv并向前传递值
假设file.csv
为:
foo,
bar
如果foo
在*json.gz
中出现了20次,bar
在*json.gz
中出现了30次,我希望命令的输出为:
20
30
- 以下是我找到的解决方案:**
while IFS=',' read -r column; do
count=$(zgrep -o "$column" *json.gz | wc -l)
echo "$column,$count"; done < file.csv
3条答案
按热度按时间cclgggtu1#
您可以通过一个
grep
操作将file.csv
视为一个 patterns 文件(每行获取一个模式)来实现这一点:-o
-仅打印匹配的部件-h
-禁止输出文件名wlwcrazw2#
您可以迭代
cat
run through子进程的输出:q3qa4bjr3#
使用chatgpt:),请尝试以下操作: