简体   繁体   中英

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:

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:

awk -F "@*" '{print $4}' data.csv > temp.csv

Then I ran some additional scripts to modify temp.csv .

Now, I need to return the contents of temp.csv to data.csv , replacing everything in the 4th column of data.csv .

How can I replace the contents of the 4th column of data.csv with the edited lines in 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:

Having first prepared your temp.csv file:

$ 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:

$ 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM