Print the characters in a word

Hi,

How can I split the characters in a word?

For Eg:

If my input is:
command

my output should be:
c
o
m
m
a
n
d

Please help me in doing it so.

Regards,
Chella

and what do you want to do after splitting them up?

I need to use these characters to find the lines in another file which starts with the characters.

Eg:
c
o
m
m
a
n
d

file2:
cat meows
dog barks
.
.
.
apple a day keeps the doctor away

so now i want to print the lines
cat meows
apple a day keeps the doctor away

Hope things are clear.

Regards,
Chella

there are better ways, but here's one for a start

#!/bin/sh
input="command"
awk -v input=$input 'BEGIN{FS=""}
{ x[$1]=$0}
END{
     n=split(input,a,"")    
     for(i=1;i<=n;i++){
	  if (x[a] ~ a) print x[a]
     }
}' "file" 

Thank You for the reply.

But still I have problem in doing it. I am not able to split the word into characters.

when I tried to print the value of n in the below case
n=split(input,a,"")
print n;
I get 1

please help me out.

Regards,
Chella

  1. With bash (or ksh93 or zsh) and fold:
$ cat file1
command
$ cat file2
cat meows
xdog barks
apple a day keeps the doctor away
$ awk 'NR==FNR{l[$1];next};substr($1,1,1) in l' <(fold -w1 file1) file2
cat meows
apple a day keeps the doctor away

or with awk only:

awk 'NR==FNR{for(i=1;i<=length;i++)l[substr($0,i,1)];next}
substr($1,1,1) in l' file1 file2

Use nawk or /usr/xpg4/bin/awk on Solaris.

P.S. I changed dog to xdog for the demonstration. Why not "dog barks"?