抓取 csv 的特定列

抓取 csv 的特定列

您能帮忙修复这个脚本吗?

$ private=False
$ min_num_followers=100
$ max_num_followers=1000
$ awk -v private="$private" '$11=private' ExportData_followers_memoryweaver_37677682_10.csv  |  awk -v min_num_followers="$min_num_followers" '$9 > min_num_followers'| awk -v max_num_followers="$max_num_followers" '$9 < max_num_followers'  |awk -F"," '{x+=$8} END {print x}'

我想要获取第 9 列 > min_num_followers 且 < max_num_followers 并且第 11 列是私有的行。 在此处输入图片描述

**无论如何,我并不偏向于使用 awk

答案1

您不需要多个awk命令 - 您可以链接逻辑条件&&(或者||如果逻辑需要它)。

您还需要用 (逻辑测试) 替换$11=private(赋值) $11==private。因此,类似这样的操作应该可行:

awk -F"," -v private="$private" -v min_num_followers="$min_num_followers" -v max_num_followers="$max_num_followers" '
  $11==private && $9 > min_num_followers && $9 < max_num_followers
' ExportData_followers_memoryweaver_37677682_10.csv

如果希望输出以逗号分隔,OFS = ","请在表达式主体awk或命令行中设置,例如

awk -F"," -v private="$private" -v min_num_followers="$min_num_followers" -v max_num_followers="$max_num_followers" '
  $11==private && $9 > min_num_followers && $9 < max_num_followers
' OFS="," ExportData_followers_memoryweaver_37677682_10.csv

答案2

类似的东西awk -F, '$9 > 500 && $9 < 1000 && $11 == "True"'应该可以工作

答案3

只需确认这两个都有效:

$ awk -F"," '$9 > 100 && $9 < 1000 && $11 == "False"' ExportData_followers_reddit_1702003595_10.csv | wc -l
1089
$ gawk -F"," '$9 > 100 && $9 < 1000 && $11 == "False"' ExportData_followers_reddit_1702003595_10.csv | wc -l
1089

相关内容