![抓取 csv 的特定列](https://linux22.com/image/891471/%E6%8A%93%E5%8F%96%20csv%20%E7%9A%84%E7%89%B9%E5%AE%9A%E5%88%97.png)
您能帮忙修复这个脚本吗?
$ 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