1

I have csv file like this:

No, Date, Expr 11, 2013, 5413 24, 2014, 1234 ... 

I have bash script and check no, date and expr values, print to csv files:

echo -e "No, Date, Expr" > "file.csv" echo -e "$no, $Date, $Expr" >> "file.csv" echo -e "$no2, $Date2, $Expr" >> "file.csv" ... 

But I need to check old data and if new value is same or new value is empty, i must not write anything. For example: if $no2=24 and old no2 in csv file is 24 too, i must not echo.

So I need to check and echo specific line. I can check values and compare new value. But how can I insert for example just 3. line 2. value in csv file with bash?

">" create new file, so its delete everything. ">>" echo last line, so its insert new values, new lines.

Csv file must be static, just values must be dynamic. How can I do?

1 Answer 1

1

You can use this awk:

awk -F ', *' -v n="$no2" -v dt="$Date2" -v expr="$Expr" -v OFS=', ' '$1==n {$0 = n OFS dt OFS expr} 1' file.csv 
Sign up to request clarification or add additional context in comments.

2 Comments

Sorry for misunderstanding, my fault. But I need to insert same line, not new line. Your example just check $no2 and print new line. I need to check for example 2. line, and insert 2. line. Like database. Csv file line number must be static.
Yes. Insert wrong word, sorry for my bad english. I need to update.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.