简体   繁体   English

如何用BASH中另一个文件中的行替换CSV文件的列?

[英]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

你也可以使用pastecut

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.

相关问题 BASH:如何将CSV文件与列标题分开显示 - BASH: How to print CSV file out with column headers as separate lines 如何在 csv 文件中查找整个句子并使用 bash 将其替换为另一个文件中的句子? - How to find an entire sentences in a csv file and replace it with sentences from another file using bash? 删除CSV文件中带有bash中列条件的行 - Delete lines in CSV file with a column condition in bash Bash:使用另一个文件的行查找和替换文件中的行 - Bash: Find and replace lines in a file using the lines of another file 使用 bash 中的另一个文件替换 txt 文件中的多行 - replace multiple lines in a txt file using another file in bash bash脚本用另一个文件中的行替换文件中的所有空值 - bash script replace all null values in a file by lines from another file Linux BASH,如何从另一个文件添加两行 - Linux BASH, how to add two lines from another file bash删除文件中包含来自另一个文件的行的行 - bash delete lines in file containing lines from another file 如何用BASH中的另一个文件中的相应行填充一个文件中的空行? - How to fill empty lines from one file with corresponding lines from another file, in BASH? Bash:如何从CSV文件的列中抓取带有“定界符”的列? - Bash: How to grab columns with “delimiter” within the column from CSV file?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM