How to shuffle odd and even columns?

Is there any way to place each even column name infront of its odd column using awk or others?

input

Ab	name	MGH26	B04	MGH26	B05

output

name_Ab	B04_MGH26	B05_MGH26

off the top of my head:

awk '{
for (i=1;i<=NF;i++) {
    if (i%2) {
        oname=$i
    } else {
        ename=$i
        printf("%s_%s ",ename,oname);
    }
}

Doesn't preserve the white spacing though.

1 Like

it works great. thanks.
note:you forgot this at end of the script.

}'
awk 'for (i=1;i<=NF; i=i+2) printf("%s%s", $(i+1) "_" $i, (i+2)<NF?FS:ORS)' myFile

The last solution is missing the { }

awk '{for (i=2;i<=NF;i+=2) printf "%s_%s%s", $i, $(i-1), (i<NF)?FS:ORS}' myFile