I'm trying to loop an AWK script that contains two conditions and a variable coming from a stated list. The purpose is to extract the line when the column one and column three meet two particular conditions (the name of the text in the two columns has to partially match) My input file is made this way:
pop1_io 1 pop1_ei 2 1 62027313 63797977 3.047
pop1_eg 1 pop2_yu 2 1 74240214 78974955 3.827
pop3_ab 1 pop1_zx 2 1 160604473 163511425 4.04
The first script I wrote works perfectly if I write manually the name of the variable I need, but it doesn't work if I try to loop it and insert variables into the awk script. Working one:
awk '{if ($1 ~ /pop1/ && $3 ~ /pop1/)
print $1"\t" $2 "\t" $3 "\t" $4"\t" $5 "\t" $6 "\t" $7 "\t" $8}' inputfile.ibd | sed -r '/^\s*$/d' > pop1.ibd
Not working ones:
pops="pop1 pop2 pop3"
for pop in $pops
do
awk '{if ($1 ~ /$pop/ && $3 ~ /$pop/)
print $1"\t" $2 "\t" $3 "\t" $4"\t" $5 "\t" $6 "\t" $7 "\t" $8}' inputfile.ibd | sed -r '/^\s*$/d' > out.$pop.ibd
done
This first script doesn't print anything. My second attempt is this:
for pop in $pops
do
awk '{if (a[$1]=~$pop && a[$3]=~$pop)
print $1"\t" $2 "\t" $3 "\t" $4"\t" $5 "\t" $6 "\t" $7 "\t" $8}' Roma_Czech.ibdne.ibd | sed -r '/^\s*$/d' > out.$pop.ibd
done
In this case it prints everything contained in the first file. I could I fix this script?