Update field value on a csv file

Hi
I have a job status csv file. I want to update the status of the job in the file.
Below is the csv file

 
1,jobname1,in_progress,starttime,somthing,somthing
2,jobname2,completed,starttime,somthing,somthing
3,jobname3,failed,starttime,somthing,somthing
4,jobname1,not_started,starttime,somthing,somthing

I want to update the status of the job in the file for a pattern "1,jobname1" from in_progress to completed. Basically i want to update the 3rd field value (what ever it may be) of the line which matches the pattern "1,jobname1" with "completed"

ie, after the update the file should look like

 
1,jobname1,completed,starttime,somthing,somthing
2,jobname2,completed,starttime,somthing,somthing
3,jobname3,failed,starttime,somthing,somthing
4,jobname1,not_started,starttime,somthing,somthing

can I do this with awk ?
Thanks

awk -F, '/1,jobname1,in_progress/ {$3="completed"} 1' filename
1 Like

Thanks for the reply

I can take only "1,jobname1" as the search string and i want update any column on that line.

ie, I would like to search with string "1,jobname1" and update 3rd column with "completed" and 5th column with date +"%Y%m%d%H%M%S"

awk -F, 'BEGIN{"date +\"%Y%m%d%H%M%S\""|getline d} /1,jobname1/{$3="completed";$5=d} 1' filename
1 Like

this is working fine for me... Thanks a lot

is it possible to pass the value in a variable. I tried the below

 
awk -F, '/1,jobname1/ {$3="completed";$5="${temp_date}"} 1' OFS="," file

but instead of assigning the value from variable temp_date, I'm getting the string "${temp_date}" in the result.

You need to use awk variable like this:

awk -F, -v td=$temp_date '/1,jobname1/ {$3="completed";$5=td} 1' OFS="," file
1 Like

Thanks, its working for me

---------- Post updated at 01:25 PM ---------- Previous update was at 05:45 AM ----------

is it possible to pass the pattern in a variable

pattern_str="1,jobname1"
awk -F, -v td=$temp_date '/$pattern_str/ {$3="completed";$5=td} 1' OFS="," file
pattern_str="1,jobname1"
awk -F, -v td=$temp_date -v pattern_str="${pattern_str}" '$0 ~ pattern_str {$3="completed";$5=td} 1' OFS="," file
1 Like

Thanks... its working