Sunday, 8 September 2013

Using an if/else statement in the middle of AWK

Using an if/else statement in the middle of AWK

I have a 5-column file:
PS 6 15 0 1 PS 1 17 0 1 PS 4 18 0 1
that I would like to get it in this 7-column format:
PS.15 PS 6 N 1 0 1 PS.17 PS 1 P 1 0 1 PS.18 PS 4 N 1 0 1
To create 6 of the 7 columns requires just grabbing directly (and
sometimes applying small arithmetic) from columns in the original file.
However, to create one column (column 4) requires an if-else statement.
Specifically, to create new columns 1, 2, 3, I use:
cat File | awk '{print $1"."$3"\t"$1"\t"$2}'
and to create new columns 5, 6,7, I use:
cat testFileB | awk '{print $4+$5"\t"$4/($4+$5)"\t"$5/($4+$5)}'
and to create new column 4, I use:
cat testFileB | awk '{if ($2 == 1 || $2 == 2 || $2 == 3) print "P"; else
print "N";}'
These three statements work fine independently and get me what I want (the
correct values for the columns that are all separated by tabs). However,
when I try to apply them simultaneously (create all 7 columns at once), I
can only do so with unwanted new lines (instead of tabs) before and after
column 4 (the if/else statement column):
For instance, my attempt to simultaneously create columns 1, 2, 3, 4:
cat File | awk '{print $1"."$3"\t"$1"\t"$2; if ($2 == 1 || $2 == 2 || $2
== 3) print "P"; else print "N";}'
results in unwanted new lines before column 4:
PS.15 PS 6 N PS.17 PS 1 P PS.18 PS 4
Similarly, my attempt to simultaneously create columns 4, 5, 6, 7:
cat File | awk '{if ($2 == 1 || $2 == 2 || $2 == 3) print "P"; else print
"N"; print $4+$5"\t"$4/($4+$5)"\t"$5/($4+$5)}'
results in unwanted new lines after column 4:
N 1 0 1 P 1 0 1 N 1 0 1
Is there a solution so that I can create all 7 columns at once, and there
are only tabs between them (no new lines)?

No comments:

Post a Comment