Find the answer to your Linux question:
Results 1 to 6 of 6
Hi, I used the following command to swap the fist 2 columns of a file which contains around 100 columns. gawk '{temp=$1;$1=$2;$2=temp}{print}' study_hm2_chr1.ped>temp1.ped mv temp1.ped study_hm2_chr1.ped I am trying to ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Oct 2011
    Location
    Seattle, WA
    Posts
    5

    swapping columns


    Hi,

    I used the following command to swap the fist 2 columns of a file which contains around 100 columns.

    gawk '{temp=$1;$1=$2;$2=temp}{print}' study_hm2_chr1.ped>temp1.ped
    mv temp1.ped study_hm2_chr1.ped

    I am trying to do this for a list of files in a loop but having no success. One method did swap the columns but deleted all the other columns which is not what I wanted. Is there a one-liner shell or perl command which will do this. This is what I am trying to do:

    for i in {1..22}; do
    SWAP 1st and 2nd columns of study_hm2_chr${i}.ped
    done


    Thanks,

    -Joey

  2. #2
    Just Joined!
    Join Date
    Aug 2006
    Posts
    16

    swappying columns the easy way

    Have you thought about just doing this?
    Code:
    for i in {1..22}; do
      awk '{print $2,$1}' study_hm2_chr${i}.ped > ped.$$
      mv ped.$$ study_hm2_chr${i}.ped
    done
    Seems like you're working too hard at it.

    John
    Last edited by John Rodkey; 11-22-2011 at 01:31 AM. Reason: more complete example

  3. #3
    Just Joined!
    Join Date
    Oct 2011
    Location
    Seattle, WA
    Posts
    5

    tried that before but didn't work

    it does swap the first two columns for me but deletes the other columns...

  4. $spacer_open
    $spacer_close
  5. #4
    Trusted Penguin
    Join Date
    May 2011
    Posts
    4,353
    If you have a known, static number of columns (let's say five), you could simply include them, e.g.:

    Code:
    awk '{print $2,$1,$3,$4,$5}' study_hm2_chr${i}.ped > ped.$$
    Or a more flexible solution might be something like:
    Code:
    cat study_hm2_chr1.ped|awk '{printf $2" "$1" "};{ for (x=3;x<=NF;x++) { printf "%s ", $x } };{print ""}'
    I'm more of a perl slinger, though, so my awk code is probably questionable.

  6. #5
    Just Joined!
    Join Date
    Oct 2011
    Location
    Seattle, WA
    Posts
    5
    Thanks. I would really like an oneliner perl code but I don't know that much. In the interim, this seemed to work, though not elegant:

    for i in {7..22}; do
    gawk '{temp=$1;$1=$2;$2=temp}{print}' study_hm2_chr${i}.ped>temp${i}.ped;
    mv temp${i}.ped study_hm2_chr${i}.ped;
    done

  7. #6
    Trusted Penguin
    Join Date
    May 2011
    Posts
    4,353
    Well, it was more elegant than my hideous awk attempt.

    How about this perl one-liner?

    Code:
    cat study_hm2_chr${i}.ped | perl -p -e 's/^(\w)[ \t]+(\w)(.*)$/$2 $1$3/'

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •