shell - 打印 CSV 的前 N ​​行,其中引用的字段可以包含换行符

标签 shell csv awk sed

CSV 文件可以包含带新行的数据。它可以与任何列。还有一些行可以有没有任何新行的数据,所以它应该适用于所有情况
样本输入

   ID,username,mobile,city,Message,Address,city
'11111111',TestUSer,1234567890,test,"Hi how are you? Well: we will connnect

Thanks for your time!
With Joy.
Test",Address test,City test
11111116,TestUser,1234567891,test,hello msg,Address test1,City test1
'111111167',TestUSer,1234567890,test,"Hi how are you one? Well: we will connnect

Thanks for your time!
With Joy.
Test",Address test,City test
11111112,TestUser,1234567891,test1,hello msg1,Address test2,City test2
11111113,TestUser,1234567891,test1,hello msg1,Address test2,City test2
11111114,TestUser,1234567891,test1,hello msg1,Address test2,City test2
我正在使用以下命令读取 csv 的前 5 条记录
awk -v RS='("[^"]*")?\r?\n' 'NF{ORS = gensub(/\r?\n(.)/, "\\\\n\\1", "g", RT);  ++n; print} n==5{exit}' file.csv
实际输出:
ID,username,mobile,city,Message,Address,city
'11111111',TestUSer,1234567890,test,"Hi how are you? Well: we will connnect\nThanks for your time!\nWith Joy.\Test",Address test,City test
11111116,TestUser,1234567891,test,hello msg,Address test1,City test1
'111111167',TestUSer,1234567890,test,"Hi how are you one? Well: we will connnect\nThanks for your time!\nWith Joy.\nTest",Address test,City test
11111112,TestUser,1234567891,test1,hello msg1,Address test2,City test2
11111113,TestUser,1234567891,test1,hello msg1,Address test2,City test2
11111114,TestUser,1234567891,test1,hello msg1,Address test2,City test2
想要的输出:
ID,username,mobile,city,Message,Address,city
'11111111',TestUSer,1234567890,test,"Hi how are you? Well: we will connnect\nThanks for your time!\nWith Joy.\Test",Address test,City test
11111116,TestUser,1234567891,test,hello msg,Address test1,City test1
'111111167',TestUSer,1234567890,test,"Hi how are you one? Well: we will connnect\nThanks for your time!\nWith Joy.\nTest",Address test,City test
11111112,TestUser,1234567891,test1,hello msg1,Address test2,City test2

最佳答案

仅凭您展示的 sample ,您能否尝试关注 awk代码。用 GNU 编写和测试 awk .利用 RS记录分隔符,然后全局替换以取消 RT 中的新行,然后相应地打印行。

awk -v RS='"[^"]*"' '{gsub(/\n/,"\\n",RT);ORS=RT} 1' Input_file
要获取前 10 条记录,请尝试以下操作:
awk -v RS='"[^"]*"' '{gsub(/\n/,"\\n",RT);ORS=RT} 1' Input_file | head -10

关于shell - 打印 CSV 的前 N ​​行,其中引用的字段可以包含换行符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68485135/

相关文章:

linux - 使用 sed 或 awk 在 linux 中删除一个单词及其下一行

linux - 使用正则表达式将列与 awk 匹配

linux - awk 按条件打印 CSV 文件的行?

shell - Unix 中的自定义排序

javascript - 被我收到的 d3 错误难住了 - 错误 : <path> attribute d: Expected number, "M0,NaNL21.923076923…"

linux - 我正在寻找将我的 MS-DOS .bat 文件转换为等效的 linux .sh 文件

java - CSV 到 XML 转换 Java

python - 读取 CSV 文件,查找具有相同值的列并附加其他列值

linux - apt-get 更新在鹦鹉操作系统中不起作用

ruby - 使用适当的参数转义从 ruby​​ 调用 shell 命令