Reputation: 63
Please tell me why this program is not working?? help me improve it.
for i in $(seq 2 30); do awk '{if ($i < 0.05) print $1,$i}' test.txt > phen_$i.txt; done
File test.txt looks like following:
name phen1 phen2
rs549 0.02 0.02
rs699 0.03 0.03
rs701 1 1
rs751 0.449 0.449
rs884 1 1
rs923 0.9775 0.02
rs970 0.9836 0.03
rs1085 0.0001 0.99
rs1220 0.9316 0.9316
output file are expected to be two different files namely phen_2.txt and phen_3.txt containing col1 and col2, and col1 and col3 respectively according to if condition.
expected outcome for phen_2.txt:
rs549 0.02
rs699 0.03
rs1085 0.0001
expected outcome for phen_3.txt:
rs549 0.02
rs699 0.03
rs923 0.02
rs970 0.03
please help!
Upvotes: 1
Views: 638
Reputation: 360733
for ((i = 2; i <= 30; i++)); do awk -v i="$i" '$i < 0.05 {print $1, $i}' test.txt > "phen_$i.txt"; done
Use variable passing (-v
) to get the shell variable into the AWK script.
You don't need seq
.
Edit:
Fixed some overzealous and bad errors on my part.
Here's how you can do the same thing completely within AWK:
awk '{for (i = 2; i <= 30; i++) {if ($i < 0.05) {print $1, $i > "phen_" i ".txt"}}}' test.txt
This only goes through the input file once, but it cycles through the set of output files for each line of input. The shell version reads the input file repeatedly, but writes to each output file once.
Upvotes: 2
Reputation: 64623
Use $'$i'
instead of $
:
for i in $(seq 2 30)
do
awk '{if ($'$i' < 0.05) print $1,$'$i'}' test.txt > phen_$i.txt
done
The problem is that your $i
from the bash cannot get into the ' '
-string. So you need to close the string before $i
and open it once again just after.
Upvotes: 1