PDA

View Full Version : awk to split on all characters in a string


paddl3r
November 27th, 2007, 07:46 AM
Dear Forum,

I am struggling to write a block of awk to take a string such as:

blah

the split on all characters and print them out:

b
l
a
h

The closest I've got is this:

#!/usr/bin/awk -f
BEGIN {
string="blah";
n=split(string,array,"[A-Z]|[a-z]");
for (i=1;i<=n;i++) {
print(array[i]);
}
exit;
}

It splits on each character but prints a blank line for each of the characters. Any suggestions would be welcome.

Cheers,
paddl3r

PS I don't have nawk or gawk on this system. My boss has insisted it slots in with an existing suite of awk scripts rather than allowing me to use perl :(

LaRoza
November 27th, 2007, 08:00 AM
I don't know awk, but tried to see how it works, by trial and error.

I found that array[] is not printable, at any index. It is my guess that the characters of the string are not in the array.




#! /usr/bin/python

name = raw_input()

for i in name:
print i


You can't use Python either?

paddl3r
November 27th, 2007, 08:05 AM
python, ruby... unfortunately not

LaRoza
November 27th, 2007, 08:07 AM
#!/usr/bin/awk -f
BEGIN {
string="blah";
n=split(string,array,"");
for (i=1;i<=n;i++) {
print(array[i]);
}
exit;
}


I don't know why it works, but trial and error.

I need to learn awk, I hate being right and not knowing why.

paddl3r
November 27th, 2007, 08:14 AM
Thanks for the post but sorry that prints same as the input string on my system:

blah

I tried "" initially thinking it would work but didn't.

LaRoza
November 27th, 2007, 08:18 AM
./c.awk
b
l
a
h


There is no space in the "". If there is a space, it prints the string.

geirha
November 27th, 2007, 08:27 AM
you can also use sed
string="blah"
echo -n $string | sed 's/./&\n/g'

Edit: Though reading a bit closer, that's not an option anyway :/

LaRoza
November 27th, 2007, 08:28 AM
you can also use sed


Unfortunately, the boss wants it the way the boss wants it.

ghostdog74
November 27th, 2007, 12:35 PM
awk (and sed) are such basic and essential tools in *nix you should get to know them better

s="blah"
echo $s | awk 'BEGIN{FS=""}{for(i=1;i<=NF;i++)print $(i)}'

or

s="blah"
echo $s | awk 'BEGIN{FS="";OFS="\n"}{$1=$1}1'

output

# ./test.sh
b
l
a
h