Bash:写入 CSV 中的特定列 [英] Bash: write to specific column in CSV

查看:31
本文介绍了Bash:写入 CSV 中的特定列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将 .txt 文件的内容写入 CSV 文件中的B"或第二列.

I am trying to write the contents of a .txt file to the "B" or second column in a CSV file.

awk '{$2 = $2"i"; print}' x.txt >> y.csv

我认为这会将 x.txt 的内容写入 y.csv 后跟第二列中的字母i".但是,此代码仍写入第一列.

I thought this would write the contents of x.txt to y.csv followed by the letter "i" in the second column. However, this code still writes to the 1st column.

x.txt 示例:

hello
hellox
hello1

样本输出到 y.csv:

Sample output to y.csv:

一列

hello i
hellox i
hello1 i

我想将此内容写入 B 列.最好没有i".

I want to have this content written to the B column. Preferably without the "i".

对此的任何解决方案将不胜感激.

Any solution to this would be appreciated.

推荐答案

你可以使用这个awk:

awk 'BEGIN{FS=OFS=","} {$2 = $1} 1' file.csv

hello,hello
hellox,hellox
hello1,hello1

如果您想在输出的第二列中使用文字 i:

If you want literal i in 2nd column of output:

awk 'BEGIN{FS=OFS=","} {$2 = "i"} 1' file.csv

hello,i
hellox,i
hello1,i

这篇关于Bash:写入 CSV 中的特定列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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