Unix File - Adding columns in the middle

Hello,

I have a comma separated flat file. It contains some 20 columns. I want to add two new columns at position 2,3. So that file will have 22 columns. I am providing here sample data with file having 4 columns. Appreciate your help in finding solution for this.

data in input file:

call_id,conn_id,result,ani,
1,100,hungup,7601234
2,101,hungup,7601235

required output

call_id,key, value,conn_id,result,ani,
1,key1,10,100,hungup,7601234
2,key2,11,101,hungup,7601235

Where the 2 new adding columns come from ?

assuming they are in a file called in3 maybe something like

cut -d"," -f1 input >in1.tmp
cut -d"," -f2- input >in2.tmp
paste -d"," - - - in1.tmp in3 in2.tmp >output
rm in?.tmp

... lazy way ... waiting for Scrutinizer to shoot an awk 1 liner :smiley:

They are just hard coded values. Actually let me correct the output data. Sorry for the confusion.

call_id,key, value,conn_id,result,ani,

1,key1,10,100,hungup,7601234
2,key1,10,101,hungup,7601235
nawk -F, '{$1=$1 OFS "key1" OFS "10";print}' OFS=, myFile

Thanks a lot. I am getting correct data.
However title for new columns is coming as key1,10. I expect it as key, value. Is there a way to get correct column title too.

 
awk -F, 'BEGIN{print "call_id,key, value,conn_id,result,ani,"}{NF+=2;for(i=NF-2;i>=2;i--)$(i+2)=$i;$2="key1";$3="10";print}' OFS=,  inputFile

put

key, value
key1,10
key2,20
key3,30
...

in a file called "in3" and give a try with what i suggest in post #2 and see if the generated output fits your needs

Code provided by vgersh99 is working fine and adding hard coded values "key1,10" for all rows. Only pending thing is showing title for those columns as "key, value". Appreciate your help.

@anurag.singh: Your 'for' cicle is not correct and you don't need it, a simple NR>1 will do the job :wink:

@ravi.videla: Try this:

awk -F, 'BEGIN{print "call_id,key, value,conn_id,result,ani,"}NR>1{$1=$1 OFS "key1" OFS "10";print}' OFS=, file

Thanks everyone. You all are very helpful and quick to reply to my question.
Now it is working perfectly as i expected. I can understand first print is to give the title row, NR>1 to consider from 2nd row. Can you please explain other portion of the code.

awk -F, '$1=$1",key"(NR>1?"1,10":",value")' OFS=, file

---------- Post updated at 21:34 ---------- Previous update was at 21:22 ----------

 sed '1s/,/,key,value,/;1!s/,/,key1,10,/' file
awk 'BEGIN{FS=OFS=","} $1=$1 (NR==1?",key, value":",key1,10")' infile