Concatenating values in a File

Hi All,

I have a ',' delimited file and i would like concatenate a new value at a specific column.

Example :-
xXXX,XYZ,20071005,ABC,DEF,123
xXXX,XYZ,20071005,ABC,DEF,123
xXXX,XYZ,20071005,ABC,DEF,123

The output that i want is
xXXX,XYZ,20071005001,ABC,DEF,123
xXXX,XYZ,20071005002,ABC,DEF,123
xXXX,XYZ,20071005003,ABC,DEF,123
........
xXXX,XYZ,20071005010,ABC,DEF,123
xXXX,XYZ,20071005011,ABC,DEF,123
and so .... on....

As you see the only difference is in the Column # 3 where i want to add 001, 002, 003 .... till the end of file [ in the above example there are 11 records so last updated value is 20071005011 ], however all other values should remain the same.

Please let me know if you have any doubts about the query.

and thanks for your time.:slight_smile:
Amit

#assumption: only till 999
awk 'BEGIN{OFS=FS=",";c=1}
    { 
     n=sprintf("%03d",c++)
     $3=$3n     
    }
    1' "file"

Hi, can you explain how it work i am not able to understand and also not able to us it... Please help
Thanks for your time.
Amit

1) 'BEGIN{OFS=FS=",";c=1} #set output and input field separator to ",", initialize c variable to 1. this will be used for counting up
2) n=sprintf("%03d",c++) # string formatting. precede the number by 0's and fix it at length 3. Then assign the value to n eg 001, 002, till 999
3)$3=$3n #append 001 , 002 etc to field 3, which is the field with date, eg 20071005

which platform are you in? i am using GNU awk.

Hi,
This should be also ok.

code:

awk 'BEGIN{FS=","}
{
print $1","$2","$3*1000+NR","$4","$5","$6
}' filename

Hi,

I am using this on Solaris Box and i tried your solution how ever the output is different and it is adding the 001 in the front side, not sure why. So the output is 00171005 for "20071005".

Will this be case of different platform...

try nawk on Solaris

Thanks guys for youre help it worked.... awk 'BEGIN{OFS=FS=",";c=1} { n=sprintf("%03s",c++); $5=$5n ;print $5}' , Only i had to use %03s instead of %03d.
Thanks