0

I have a script in bash that in some part of the code i need to add to a csv specific information in row and column specific, for example :

The format of my csv is the next :

Columns1 Columns2 Columns3 Columns4 Columns5 Columns6 Columns7 Columns8
animals   value2   value3   value4   value5   value6   value7   value8
bird      value2   value3   value4   value5   value6   value7   value8

So, I need that when the script found a value for example :

grep "animals" myfile.csv

add some information for example in the row with the value "animals" add or replace the value in the column5

In all my script.sh always I need to add several information many times in a specific range.

I would appreciate that the solution was in bash

1

1 Answer 1

1

You can do this very easily with awk:

awk -v M=match -v R=replace 'BEGIN {FS=","; OFS=","} $1==M {$5=R} {print}' infile.csv > outfile.csv
Sign up to request clarification or add additional context in comments.

8 Comments

Woow this is works, really, I had been searching for days this answer, but only one little detail, when i run this command in a terminal , it works but does no add the information to the csv file, what parameter i need to add ?
I just updated the code to show how to write the output to a new file. You can then rename the new file to the same name as the old file if you'd like.
ok, i see, but i have a little problem, after executed this comand the line where is replace a value, it is shows without "," , like this : animals value2 value3 value4 newvaluel value6 value7 value8 and when create a new file (> outfile.csv) i have the value of the row modified in a cell instead of severals cells
Sorry about that. I've edited it so that both the input and output field separators are set properly.
Thanks again Misandrist
|

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.