Thursday, July 23, 2015

Reading Unix variables within awk

If you want to use Unix variables within the awk code use "'" (double quote - single quote - double quote) to enclose the variable.

In the following parsing code, Unix variable $chr is called within awk using double-single-double quotes:

for chr in $(seq 1 22) ; do
echo "parsing $chr"
awk -F"\t" '$1 == "'"$chr"'" { print $2"\t"$3 }' hg18_avg_occ.standard > chr"$chr"
done


The code will loop $chr variable from 1 to 22, test if column one equals $chr within awk, and print columns 2 and 3 to a file containing $chr in its name. Note that $chr contains double-single-double quotes within awk but not when output is sent to a file chr"$chr" (where single quotes are sufficient).
 
head  hg18_avg_occ.standard
1       1       0.011
1       2       0.0197
1       3       0.0281
1       4       0.041
1       5       0.0573
1       6       0.0773
1       7       0.0917
1       8       0.103
1       9       0.112
1       10      0.124


head chr1
1       0.011
2       0.0197
3       0.0281
4       0.041
5       0.0573
6       0.0773
7       0.0917
8       0.103
9       0.112
10      0.124


No comments:

Post a Comment