Visit Our UNIX and Linux User Community


Print the characters in a word


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Print the characters in a word
# 1  
Old 10-17-2007
Question 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
# 2  
Old 10-17-2007
and what do you want to do after splitting them up?
# 3  
Old 10-17-2007
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
# 4  
Old 10-17-2007
there are better ways, but here's one for a start
Code:
#!/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[i]] ~ a[i]) print x[a[i]]
     }
}' "file"

# 5  
Old 10-17-2007
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
# 6  
Old 10-17-2007
1. With bash (or ksh93 or zsh) and fold:

Code:
$ cat file1
command
$ cat file2
cat meows
xdog barks
apple a day keeps the doctor away

Code:
$ 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:

Code:
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"?

Previous Thread | Next Thread
Test Your Knowledge in Computers #1004
Difficulty: Medium
Hopper College at Caltech University was named in honor of Grace Hopper in 2017.
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Search for a specific word and print only the word from the input file

Hi, I have a sample file as shown below, I am looking for sed or any command which prints the complete word only from the input file. Ex: $ cat "sample.log" I am searching for a word which is present in this file We can do a pattern search using grep but I need to cut only the word which... (1 Reply)
Discussion started by: mohan_kumarcs
1 Replies

2. Shell Programming and Scripting

[Solved] Search for a word and print the next word

Hi, I am trying to search for a word and print the next word. For example: My text is "<TRANSFORMATION TYPE ="Lookup Procedure">" I am searching for "TYPE" and trying to print ="Lookup Procedure" I have written a code like following: echo $line | nawk... (4 Replies)
Discussion started by: sampoorna
4 Replies

3. Shell Programming and Scripting

Search for the word and exporting 35 characters after that word using shell script

I have a file input.txt which have loads of weird characters, html tags and useful materials. I want to display 35 characters after the word "description" excluding weird characters like $&lmp and without html tags in the new file output.txt. Help me. Thanx in advance. I have attached the input... (4 Replies)
Discussion started by: sachit adhikari
4 Replies

4. Shell Programming and Scripting

Search for the word and exporting 35 characters after that word using shell script?

I have a file input.txt which have loads of weird characters, html tags and useful materials. I want to display 35 characters after the word description excluding weird characters like $$#$#@$#@***$# and without html tags in the new file output.txt. Help me. Thanx in advance. My final goal is to... (11 Replies)
Discussion started by: sachit adhikari
11 Replies

5. Shell Programming and Scripting

perl lwp find word and print next word :)

hi all, I'm new there, I'm just playing with perl and lwp and I just successfully created a script for log in to a web site with post. I have a response but I would like to have something like this: I have in my response lines like: <div class="sender">mimi020</div> <some html code.....>... (3 Replies)
Discussion started by: vogueestylee
3 Replies

6. Shell Programming and Scripting

How ti Grep for a word and print the next word

Hi can we grep for a word and print the next word of the greped word? ex:- create or replace function function_name create function function_name we should search for word "function" and output next word "function_name" from both lines. (3 Replies)
Discussion started by: manasa_vs
3 Replies

7. UNIX for Dummies Questions & Answers

How to print line starts with specific word and contains specific word using sed?

Hi, I have gone through may posts and dint find exact solution for my requirement. I have file which consists below data and same file have lot of other data. <MAPPING DESCRIPTION ='' ISVALID ='YES' NAME='m_TASK_UPDATE' OBJECTVERSION ='1'> <MAPPING DESCRIPTION ='' ISVALID ='NO'... (11 Replies)
Discussion started by: tmalik79
11 Replies

8. UNIX for Dummies Questions & Answers

Script to search for a particular word in files and print the word and path name

Hi, i am new to unix shell scripting and i need a script which would search for a particular word in all the files present in a directory. The output should have the word and file path name. For example: "word" "path name". Thanks for the reply in adv,:) (3 Replies)
Discussion started by: virtual_45
3 Replies

9. Shell Programming and Scripting

deleting last characters of a word

Hi All is there a way to delete last n characters from a word like say i have employee_new i want to delete _new. and just get only employee I want this in AIX Shell scripting Thanks (3 Replies)
Discussion started by: rajaryan4545
3 Replies

10. UNIX for Dummies Questions & Answers

how to get first two characters from a word

Hi guyz, suppose there is a variable a=sachin. I want to have letter 'c' from variable a. I am trying this awk 'substr(sachin,2,1)' but its not working. I cant user cut as it requires a file whereas i have a variable. (4 Replies)
Discussion started by: sachin.gangadha
4 Replies

Featured Tech Videos