Believe it or not, I can\'t find the answer to what I would think would be this very basic question.
In awk, how can I loop over the input string character by charac
if you have gawk:
awk '$0=gensub(/(.)/,"\\1\n","g")' file
test:
kent$ echo "I am a String"|awk '$0=gensub(/(.)/,"\\1\n","g")'
I
a
m
a
S
t
r
i
n
g
By default in awk the Field Separator (FS) is space or tabs. Since you mentioned you wanted to loop over each character and not word, we will have to redefine the FS to nothing. Something like this -
[jaypal:~/Temp] echo "here is a string" | awk -v FS="" '
{for (i=1;i<=NF;i++) printf "Character "i": " $i"\n"}'
Character 1: h
Character 2: e
Character 3: r
Character 4: e
Character 5:
Character 6: i
Character 7: s
Character 8:
Character 9: a
Character 10:
Character 11: s
Character 12: t
Character 13: r
Character 14: i
Character 15: n
Character 16: g
Not all awk implementations support the above solutions. In that case you could use substr:
echo here is a string | awk '{
for (i=0; ++i <= length($0);)
printf "%s\n", substr($0, i, 1)
}'
P.S. In some awk implementations length without arguments defaults to $0, i.e. length and length($0) are equivalent.
You can convert a string to an array using split:
echo "here is a string" | awk '
{
split($0, chars, "")
for (i=1; i <= length($0); i++) {
printf("%s\n", chars[i])
}
}'
This prints the characters vertically, one per line.