【问题标题】:Copying one full column of a CSV file and adding it to another file using shell script复制 CSV 文件的一整列并使用 shell 脚本将其添加到另一个文件
【发布时间】:2018-10-25 17:58:25
【问题描述】:

我有一个 8 列的 tsv 文件,只需将第 7 列剪切并粘贴到另一个 txt 文件中

in.tsv:

API    Admin1    Admin2    Admin3    Admin4    Admin5    Request    Response_Code
v1/customers    200    401    401    401    401    { "customer": { "name": "abc", "email":"mailme2@xxx.com","account_classification": "xyz"} }    200

我尝试过以下命令:

paste -d, in.tsv |  awk -F, -v OFS=',' '{print $7}'

cat in.tsv | cut -d \, -f 7 > out.txt

awk -F"," '{print $7}' in.tsv > out.txt

使用上述命令,我只能复制第一行。

输出应该和 in.tsv 一样

out.txt:

{ "customer": { "name": "abc", "email":"mailme2@xxx.com","account_classification": "xyz"} }

【问题讨论】:

  • 您的输入看起来像 tsv,而不是 csv
  • @oguzismail:也许 csv 文件实际上是逗号分隔的,此处粘贴不正确,或者是空格分隔。你不能确定它是tsv
  • @satheesh :错误并不完全清楚。我认为您只想查看第七列,但您会得到其他东西。你得到了什么?
  • 文件类型将是 CSV 而不是 TSV,我已经共享了将“逗号”替换为“空格”的输入文件,我只想复制第 7 列的完整而不转义字符
  • API,Admin1 ,Admin2 ,Admin3 ,Admin4 ,Admin5 ,Request ,Response_Code,, v1/customers,200,401,401,401,401,{ "customer": { "name": "abc", "email":" mailme2@xxx.com","account_classification": "xyz"} },200 v1/customers,200,401,401,401,401,{ "customer": { "name": "def", "email":"mailme2@xxx.com", "account_classification": "xyz"} },200

标签: bash shell csv


【解决方案1】:

响应内部有一个,,因此字段 7 将在此时停止。
当您不需要真正的解析器时,您可以删除前 6 个和最后一个字段。

测试数据:

API,Admin1 ,Admin2 ,Admin3 ,Admin4 ,Admin5 ,Request ,Response_Code,,
v1/customers,200,401,401,401,401,{ "customer": { "name": "abc", "email":"mailme2@xxx.com","account_classification": "xyz"} },200
v1/customers,200,401,401,401,401,{ "customer": { "name": "def", "email":"mailme2@xxx.com","account_classification": "xyz"} },200
v1/customers,200,401,401,401,401,{ "customer": { "name": "g,h", "email":"mailme2@xxx.com","account_classification": "xyz"} },200

命令:

sed -r 's/([^,]*,){6}//;s/,[^,]*$//' testdata

备注: 标题行有一个附加的,,因此将显示在不正确的字段 7 中。

【讨论】:

  • 你能帮忙解决这个问题吗stackoverflow.com/questions/53019333/…
  • 不删除前6个和最后一个字段,如何将前6个和最后一个字段移动到另一个文件
  • 这是另一个问题,您可以通过awk 尝试。像awk -v out="anotherfile" -F, '{for (i=1;i<7;i++) printf( "%s,", $i) >>out; print $NF >> out}' in.tsv 这样的东西。您可以为“字段 7”添加类似 (i=7; i<NF;i++) 的内容。
  • 非常感谢,最后一个问题 - 有没有办法从第 1 列到第 6 列而不是第 7 列中删除逗号 - v1/customers?customer_id=0,200,401,401,401,401,{ "customer": { "name": "abc", "email":"mailme2@xxx.com","account_classification": "xyz"} } 输出需要像 v1/customers?customer_id=0 200 401 401 401 401 { "customer": { “名称”:“abc”,“电子邮件”:“mailme2@xxx.com”,“account_classification”:“xyz”} }
  • sed 's/,/ /g' 这是用空格替换逗号的命令,但我希望这应该从第 1 列到第 6 列发生
猜你喜欢
  • 2013-09-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多