Append data with substring of nth column fields using awk

Hi guys,
I have problem to append new data at the end of each line of the files where it takes whole value of the nth column. My expected result i just want to take a specific value only. This new data is based on substring of 11th, 12th 13th column that has comma seperated value.

My code:

awk -F "," '{print $0 "," $11 "," $12 "," $13}'

Input file id.csv:

A12345,1-4N,1-77,1-15,Name,1-77,ABC,First,John,1-77,0.1 /ser/tel 77777 0,0.1 /id 40001,0.1 /gen 111234567 0,2016-10-24 14:18:58
A67899,1-4U,1-78,1-20,Owner,1-66,XYZ,Last,Jane,1-77,0.1 /ser/web 88881 0,0.1 /ic 40002,0.1 /sac 222345678 0,2016-10-24 14:18:58

Expected Output:

A12345,1-4N,1-77,1-15,Name,1-77,ABC,First,John,1-77,0.1 /ser/tel 77777 0,0.1 /id 40001,0.1 /gen 111234567 0,2016-10-24 14:18:58,77777,/ser/tel,40001,/id,111234567
A67899,1-4U,1-78,1-20,Owner,1-66,XYZ,Last,Jane,1-77,0.1 /ser/web 88881 0,0.1 /ic 40002,0.1 /sac 222345678 0,2016-10-24 14:18:58,88881,/ser/web,40002,/ic,222345678

Thanks :slight_smile:

Try

awk -F "," '{split ($11, T1, " "); split ($12, T2, " "); split ($13, T3, " "); print $0, T1[3], T1[2], T2[3], T2[2], T3[3]}' OFS="," file
A12345,1-4N,1-77,1-15,Name,1-77,ABC,First,John,1-77,0.1 /ser/tel 77777 0,0.1 /id 40001,0.1 /gen 111234567 0,2016-10-24 14:18:58,77777,/ser/tel,40001,/id,111234567
A67899,1-4U,1-78,1-20,Owner,1-66,XYZ,Last,Jane,1-77,0.1 /ser/web 88881 0,0.1 /ic 40002,0.1 /sac 222345678 0,2016-10-24 14:18:58,88881,/ser/web,40002,/ic,222345678
1 Like

Hello null7,

Could you please try following and let me know if this helps.

awk -F',| ' '{print $0,$13,$12,$17,$16,$20}' OFS=,   Input_file

Thanks,
R. Singh

1 Like

Hi RudiC, thank you for your help.. but I've problem where my last value seems to appear as exponential notation. I need to use the value in my script rather than opened it in excel and change the format. Can this be fixed?

My output:

A12345,1-4N,1-77,1-15,Name,1-77,ABC,First,John,1-77,0.1 /ser/tel 77777 0,0.1 /id 40001,0.1 /gen 111234567 0,2016-10-24 14:18:58,77777,/ser/tel,40001,/id,1.11235e+08
A67899,1-4U,1-78,1-20,Owner,1-66,XYZ,Last,Jane,1-77,0.1 /ser/web 88881 0,0.1 /ic 40002,0.1 /sac 222345678 0,2016-10-24 14:18:58,88881,/ser/web,40002,/ic,222345678

Thanks

Try (untested)

awk -F "," '{split ($11, T1, " "); split ($12, T2, " "); split ($13, T3, " "); print $0, T1[3], T1[2], T2[3], T2[2], T3[3]}' OFS="," OFMT="%.0f" file