Inserting new fields to a csv file

hi
I have a csv file with few rows

 
> cat job_stat
1,jobname1,somthing,somthing
2,jobname2,somthing,somthing
3,jobname3,somthing,somthing
4,jobname4,somthing,somthing

I want to add few columns after the 2nd column and then append rest of the columns after the 3rd newly added column.
After adding fields the file should look like this

 
1,jobname1,status,starttime,somthing,somthing
2,jobname2,status,starttime,somthing,somthing
3,jobname3,status,starttime,somthing,somthing
4,jobname4,status,starttime,somthing,somthing

I can do this by iterating line through the lines

(echo $string | cut -d "," -f -2) appended with ('status,starttime,') appended with (echo $string | cut -d "," -f 3-)

redirecting to a new file and renaming the new file to original file once all the lines are processed.
Is there any easy way to do this in awk or sed.

Try (untested):

awk -F, '{$2=$2 FS "status,starttime"} 1' OFS="," file
1 Like

i tried and got the result as

 
: awk -F, '{$2=$2 FS "status,starttime"} 1' test.dat
1 jobname1,status,starttime somthing somthing
2 jobname2,status,starttime somthing somthing
3 jobname3,status,starttime somthing somthing
4 jobname4,status,starttime somthing somthing

some comas are missing from the original file

---------- Post updated at 04:18 AM ---------- Previous update was at 04:15 AM ----------

I think i missed OFS="," from the first post. Now I'm able to get the result as intended.

Thanks

You did remove this OFS=","

1 Like