2

I have a 4-column CSV file, data.csv using "@" as the separator, e.g.:

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?

2 Answers 2

4

You can also use paste and cut:

cut -d@ -f1-3 data.csv | paste -d@ - temp.csv
Sign up to request clarification or add additional context in comments.

Comments

3

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

1 Comment

Or, even easier, sed 's/\(.*@\)\(.*\)ocean\(.*\)/\1\2lava\3/g' data.csv (no need for a temporary file).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.