To iterate over a list of characters that are known at the time of writing the program (in this example, the characters are "X", "Y", "Z"):
for (i = 1; i <= 3; ++i) {
c = substr("XYZ", i, 1)
# do something with the character
}
Question: Is there a more awk-y way of doing this? Note that this is not the same as this question, as the characters I want to iterate over are not a part of the input.
To put it in context, I need to count the occurences of X, Y and Z on a particular position in a line over all lines. The input should consist only of X, Y and Zs on lines of the same length:
$ cat input.txt
XYXXXYZZYXY
XXXYYYZYYZY
YZZZZYZZXZZ
XXZXXYYZXZY
$ foo.awk < input.txt
X 3 2 2 2 2 0 0 0 2 1 0
Y 1 1 0 1 1 4 1 1 2 0 3
Z 0 1 2 1 1 0 3 3 0 3 1
This is foo.awk
at the moment:
#!/bin/awk -f
BEGIN {
FS = ""
}
NR == 1 {
len = NF
}
{
for (i = 1; i <= NF; ++i)
++profile[$i][i]
}
END {
for (c = 1; c <= 3; ++c) {
char = substr("XYZ", c, 1)
printf "%s", char
for (i = 1; i <= len; ++i)
printf " %d", profile[char][i]
printf "\n"
}
}
I have not used awk before so probably my whole approach is totally wrong.