Copy a column to another column in UNIX fixedwidth file

Hi All,

I have a fixedwidth file of length 3000. Now i want to copy a column of 4 chars i.e( length 1678-1681) to column 1127 � 1171 to the same file.
Please let me know how can i achive using a single command in fixed width file.
Also source column length is 4 chars and target column length is 45 chars.
So while coying the column the value should be left adjusted to the column.

thanks,
Kiran

Hi,

Try something like this,

 awk -v ORS='' '{l=substr($0,0,1126);r=substr($0,1172,length($0));print l;printf("%-45s",substr($0,1678,4));print r,"\n";}' input_file

Cheers,
Ranga:)

Not sure if all systems / awk versions will support that large numbers which might be well beyond LINE_MAX (check with getconf LINE_MAX -> 2048 ). I didn't test this one with looong lines, you may try yourself:

$ awk '{for (i=to;i<to+4;i++)$i=$(i-back)}1' to=30 back=10 FS="" OFS="" file

It replaces 4 chars at the "to" location with the chars "back" earlier... In your request, "to" would be 1127, and "back" were -551 ... you may want to play around with the signs and operators...

Thanks Ranga.... your command working fine and i redirected to new file.

awk -v ORS='' '{l=substr($0,0,1126);r=substr($0,1172,length($0));print l;printf("%-45s",substr($0,1678,4));print r,"\n";}' 121128120912_C0003109_GL1025.dtr > 121128120912_C0003109_GL10251.dtr

But in the original file when i press end it is showing as 3001 but in the new generated file it is showing as 3002. Please let me know what could be the reason.

thanks,
Kiran

Hey,

Please use code tags for codes and data sample. You can find the code tag icon in editor. The code tag icon look like ->

Coming to point,

The reason behind the line mismatch may be new line character.

Use the

diff

or some other file compare tool to find out the real root cause.

Cheers,
Ranga:)