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 ...
- 11-21-2011 #1Just 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
- 11-22-2011 #2Just Joined!
- Join Date
- Aug 2006
- Posts
- 12
swappying columns the easy way
Have you thought about just doing this?
Seems like you're working too hard at it.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
JohnLast edited by John Rodkey; 11-22-2011 at 12:31 AM. Reason: more complete example
- 11-22-2011 #3Just 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...
- 11-22-2011 #4Linux Guru
- Join Date
- May 2011
- Posts
- 1,838
If you have a known, static number of columns (let's say five), you could simply include them, e.g.:
Or a more flexible solution might be something like:Code:awk '{print $2,$1,$3,$4,$5}' study_hm2_chr${i}.ped > ped.$$
I'm more of a perl slinger, though, so my awk code is probably questionable.Code:cat study_hm2_chr1.ped|awk '{printf $2" "$1" "};{ for (x=3;x<=NF;x++) { printf "%s ", $x } };{print ""}'
- 11-22-2011 #5Just 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
- 11-22-2011 #6Linux Guru
- Join Date
- May 2011
- Posts
- 1,838
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/'


Reply With Quote