[英]How to replace a column of a CSV file with lines from another file in BASH?
I have a 4-column CSV file, data.csv
using "@" as the separator, eg: 我有一个4列的CSV文件,
data.csv
使用“@”作为分隔符,例如:
1@fish@ocean@likes to swim in the ocean
2@whale@ocean@likes to swim in the ocean
To edit just the 4th column, I used this command: 要仅编辑第4列,我使用了以下命令:
awk -F "@*" '{print $4}' data.csv > temp.csv
Then I ran some additional scripts to modify temp.csv
. 然后,我运行了一些其他脚本来修改
temp.csv
。
Now, I need to return the contents of temp.csv
to data.csv
, replacing everything in the 4th column of data.csv
. 现在,我需要将
temp.csv
的内容返回到data.csv
,替换data.csv
的第4列中的data.csv
。
How can I replace the contents of the 4th column of data.csv
with the edited lines in temp.csv
? 我怎样才能更换的第四列的内容
data.csv
与编辑后的行temp.csv
?
你也可以使用paste
和cut
:
cut -d@ -f1-3 data.csv | paste -d@ - temp.csv
I upvoted the other answer, but to give you an idea of the approach in awk: 我赞成其他答案,但是让您对awk中的方法有所了解:
Having first prepared your temp.csv
file: 首先准备好
temp.csv
文件:
$ cut -d@ -f4 data.csv | sed -e 's,ocean,lava,g' > temp.csv
You then read a line from temp.csv
as you read each line from data.csv
, overwriting the fourth field in the line: 然后,当您从
temp.csv
读取每一行时,从data.csv
读取一行,覆盖该行中的第四个字段:
$ awk -F@ -vOFS=@ '{ getline $4 < "temp.csv" ; print }' data.csv
1@fish@ocean@likes to swim in the lava
2@whale@ocean@likes to swim in the lava
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.