The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
How to print the word ss_psm UNIX for Dummies Questions & Answers 0 03-17-2008 02:22 PM
how to get first two characters from a word sachin.gangadha UNIX for Dummies Questions & Answers 4 12-06-2007 04:57 PM
Using first word and print their contents using awk cdfd123 Shell Programming and Scripting 6 10-10-2007 03:12 AM
Word Count without any characters but the Output String JP_II UNIX for Dummies Questions & Answers 3 06-07-2006 08:19 AM
Print out just a word from the file Kinki Shell Programming and Scripting 8 02-20-2005 11:40 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 10-17-2007
chella chella is offline
Registered User
  
 

Join Date: Oct 2007
Posts: 75
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 (permalink)  
Old 10-17-2007
ghostdog74 ghostdog74 is offline Forum Advisor  
Registered User
  
 

Join Date: Sep 2006
Posts: 2,513
and what do you want to do after splitting them up?
  #3 (permalink)  
Old 10-17-2007
chella chella is offline
Registered User
  
 

Join Date: Oct 2007
Posts: 75
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 (permalink)  
Old 10-17-2007
ghostdog74 ghostdog74 is offline Forum Advisor  
Registered User
  
 

Join Date: Sep 2006
Posts: 2,513
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 (permalink)  
Old 10-17-2007
chella chella is offline
Registered User
  
 

Join Date: Oct 2007
Posts: 75
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 (permalink)  
Old 10-17-2007
radoulov's Avatar
radoulov radoulov is offline Forum Staff  
addict
  
 

Join Date: Jan 2007
Location: Варна, България / Milano, Italia
Posts: 2,847
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"?
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 08:15 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0