"grep"包含多行字段的csv文件? [英] "grep" a csv file including multi-lines fields?

查看:349
本文介绍了"grep"包含多行字段的csv文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

file.csv:

XA90;"standard"
XA100;"this is
the multi-line"
XA110;"other standard"

我想像这样grep"XA100"条目:

I want to grep the "XA100" entry like this:

grep XA100 file.csv

获得此结果:

XA100;"this is
the multi-line"

但是grep只返回一行:

but grep return only one line:

XA100;"this is

source.csv包含3个条目. "XA100"条目包含一个多行字段. 而且grep似乎不是"grep" CSV文件(包括多行字段)的正确工具.

source.csv contains 3 entries. The "XA100" entry contain a multi-line field. And grep doesn't seem to be the right tool to "grep" CSV file including multilines fields.

你知道这份工作的方法吗?

Do you know the way to make the job ?

现实世界文件包含许多列.研究的术语可以在任何列中(不在行的开头或字段的开头).所有字段都用封装.任何字段都可以包含多行,从1行到任何行,这都是无法预测的.

the real world file contains many columns. The researched term can be in any column (not at begin of line, nor at the begin of field). All fields are encapsulated by ". Any field can contain a multi-line, from 1 line to any, and this cannot be predicted.

推荐答案

使用PS响应,此示例适用于以下示例:

Using PS response, this works for the small example:

sed 's/^X/\n&/' file.csv | awk -v RS= '/XA100/ {print}'

对于我的现实世界CSV文件,该列包含许多列,在任何地方都具有研究术语,并且多行数未知,用字符替换为",用多行以开始",所有字段均用封装" ,这有效.请注意,在sed部分中排除了第二个字符"

For my real world CSV file, with many columns, with researched term anywhere, with unknown count of multi-lines, with characters " replaced by "", with multi-lines lines beginning with ", with all fields encapsulated by ", this works. Note the exclusion of the second character " in sed part:

sed 's/^"[^"]/\n&/' file.csv | awk -v RS= '/RESEARCH_TERM/ {print}'

因为任何条目的第一列都不能以"开头.第一列始终看起来像"XXXXXXXXX",其中X是除."之外的任何字符.

Because first column of any entry cannot start with "". First column allways looks like "XXXXXXXXX", where X is any character but ".

非常感谢大家的答复,也许其他解决方案也可以使用,具体取决于您使用的CSV文件格式.

Thank you all for so much responses, maybe others solutions are working depending the CSV file format you use.

这篇关于"grep"包含多行字段的csv文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆