csv - 使用 AWK 搜索 .CSV 文件 - 仅使用第一行

标签 csv unix ubuntu awk

我一直在尝试搜索 .csv 文件的特定列以查找包含特定单词的单元格。但是,它仅适用于我的 .csv 文件中的第一行(即标题)。

该文件是一系列超过 10,000 个论坛帖子,第 1 列作为帖子关键字,第 2 列作为帖子文本。下面的标题是“关键”、“注释句”。

key,annotated sentence
"(212, 2)","Got evidence to back that up??

I'm not sure how a stoner's worse than an alcoholic really.

-Wez"
"(537, 5)","Forgive me for laughing; no, not really ha, ha, ha ha ha
Could it be that people here as well as Canada and the rest of the world has figured out your infantile ""grading system of States"" is a complete sham and your very reason for existing is but an anti-constitutional farce and has lost any claims you have or will make? You stand alone now brady, with simply a few still clinging to the false hope of having others pay for your failures and unConstitutional stance so you can sit on your hands while you keep harping on overturning the 2A."
"(595, 0)",So you're actually claiming that it is a lie to say that the UK has a lower gun crime rate than the US? Even if the police were miscounting crimes it's still a huge and unjustified leap in logic to conclude from that that the UK does not have a lower gun crime rate.
"(736, 3)","The anti-abortionists claim a load of **** on many issues. I don't listen to them. To put the ""life"" of an unfertilized egg above that of a person is grotesquely sick IMO. I support any such stem cell research wholeheartedly."

CSV 分隔符是逗号,文本分隔符是 ".

如果我尝试:
awk -F, '$1 ~ /key/ {print}' posts_file.csv > output_file.csv

它将输出标题行没问题。但是,我尝试过:
awk -F, '$1 ~ /212/ {print}' posts_file.csv > output_file.csv
awk -F, '$2 ~ /Canada/ {print}' posts_file.csv > output_file.csv

这些都不起作用 - 尽管应该有,但没有找到匹配项。我想不通为什么?有任何想法吗?提前致谢。

最佳答案

awk救援!

一般来说,复杂的 csv 不起作用,但在您的情况下,因为键和带注释的句子具有非常不同的值类型,您可以将模式搜索扩展到整个记录而不是键和值,诀窍是定义记录,这再次基于您的格式也可以。例如

$ awk -v RS='\n"' '/Canada/{print RT $0}' csv    

"(537, 5)","Forgive me for laughing; no, not really ha, ha, ha ha ha
Could it be that people here as well as Canada and the rest of the world has figured out your infantile ""grading syst
em of States"" is a complete sham and your very reason for existing is but an anti-constitutional farce and has lost a
ny claims you have or will make? You stand alone now brady, with simply a few still clinging to the false hope of havi
ng others pay for your failures and unConstitutional stance so you can sit on your hands while you keep harping on ove
rturning the 2A."

和这个
$ awk -v RS='\n"' '/(212, 2)/{print RT $0}' csv       

"(212, 2)","Got evidence to back that up??

I'm not sure how a stoner's worse than an alcoholic really.

-Wez"

关于csv - 使用 AWK 搜索 .CSV 文件 - 仅使用第一行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35786113/

相关文章:

linux - 如何在 Fedora 10 中将 .rpm 文件转换为 .deb 文件

docker - 无法启动logstash

node.js - 创建 CSV 文件时,标题值在每行中重复

postgresql - Postgres COPY TO NULL 整数

database - 检查列表中是否存在某个字段,该列表也是数据库的一部分?

linux - Linux 中的 vm.overcommit_ratio 是什么?

python - 将 RDD 写入带有拆分列的 csv

Python 分析和更改数据值

bash - 带步进增量的大括号扩展不起作用 bash

java - 在 lightdm 中从标准输入读取密码