Search code examples
awksedvim

awk/sed: repeat 1st column for each column


I am trying to append 1st column to every column as follows.

existing A B C D E F

required: B A C A D A E A F A

I have tried this for a smaller set of columns as follows;

cat file | awk '{print $2" "$1" "$3" "$1" "$4" "$1}"

How to repeat the above for more than 50 columns

Thanks, Eashwar


Solution

  • Using awk:

    $ echo "A B C D E F" | awk '{for(i=2;i<=NF;i++) printf "%s", $i" "$1" "; printf "\n"}'
    B A C A D A E A F A