copy contents of one column in another

Hi,

I want to pick contents of a column in a file and copy the contents of this to other column.

awk can be used for this, but the number of fields are higher so awk will not help. Any other way to do this.

e.g following file has some contents a follows

a,b,c,d,e,f,9,0 

i need to copy contents of second column(b here) to 7th field(9 here).

Do you want to overwrite the seventh field, or add a field?

perl -laF, -ne 'splice(@F,6,0,$F[1]); print join (",", @F)' file

This is the moral equivalent of "cut -f1-6,2,7-" except regrettably cut doesn't work that way. I used to have a "perl cut" called pcut in my ~/bin but it didn't get used all that much.

If you want to replace the seventh field instead, the third element in the splice controls that; change it to a 1 to replace instead of add. (Array indices in Perl are zero-based, that's why you see 6 and $F[1] instead of 7 and $F[2].)

Thanks for the response but another issue here is that perl is not there :mad:

Do we have some solution using sed or some other way?

I just need to replace. No need to retain the value for 7th field.

Why not?

awk -F, '$7=$2' input > output

Your comment that the field numbers in awk are insufficient lead me to suspect that this is not going to be very sustainable, but for what it's worth, here's a sed solution.

sed 's/^\([^,]*,\([^,]*,\)[^,]*,[^,]*,[^,]*,[^,]*,\)[^,]*,/\1\2/' file

There are some variations in sed syntax; if you're lucky, you could even get one which understands

sed 's/^([^,]*,([^,]*,)([^,]*,){4})[^,]*,/\1\2/' file

without those pesky backslashes, and with the neat {4} repetition count.

I'd still concur that you probably want to use awk for this if it's a real problem and the field numbers are larger than half a dozen. Probably you can work around this issue even if your awk is ... old and grumpy.