Scan text file to find all words of 4 characters or less

Please support our Shell Scripting advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Dec 2005
Posts: 2
Reputation: darklord is an unknown quantity at this point 
Solved Threads: 0
darklord darklord is offline Offline
Newbie Poster

Scan text file to find all words of 4 characters or less

 
0
  #1
Dec 8th, 2005
What I am trying to do is to scan a text file word by word and find all words of 4 character or less then output these words to a file, but the problem is I don't know a unix command that can scan the file this way. The Grep command only enable me to find a certain word in a file. Any help is appreciated
Reply With Quote Quick reply to this message  
Join Date: May 2004
Posts: 178
Reputation: jim mcnamara is on a distinguished road 
Solved Threads: 10
jim mcnamara jim mcnamara is offline Offline
Junior Poster

Re: Scan text file to find all words of 4 characters or less

 
0
  #2
Dec 8th, 2005
try awk. This is a start:
Shell Scripting Syntax (Toggle Plain Text)
  1. awk '{
  2. for(i=1;i<=NF;i++)
  3. {
  4. if(length($i)<5)
  5. {print $i}
  6. }
  7. }' filename > newfilename
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 2
Reputation: darklord is an unknown quantity at this point 
Solved Threads: 0
darklord darklord is offline Offline
Newbie Poster

Re: Scan text file to find all words of 4 characters or less

 
0
  #3
Dec 9th, 2005
Originally Posted by jim mcnamara
try awk. This is a start:
Shell Scripting Syntax (Toggle Plain Text)
  1. awk '{
  2. for(i=1;i<=NF;i++)
  3. {
  4. if(length($i)<5)
  5. {print $i}
  6. }
  7. }' filename > newfilename
Thank you.

Taking this step further. How can I filter out these 4 or less characters words, so that the newfilename only contain palindromes words?
Reply With Quote Quick reply to this message  
Join Date: May 2004
Posts: 178
Reputation: jim mcnamara is on a distinguished road 
Solved Threads: 10
jim mcnamara jim mcnamara is offline Offline
Junior Poster

Re: Scan text file to find all words of 4 characters or less

 
0
  #4
Dec 12th, 2005
We are now clearly doing homework.

This is the limit of help->
1. redirect the output of the awk script into a loop instead of into a file.
Shell Scripting Syntax (Toggle Plain Text)
  1. for word in `awk script above`
  2. do
  3.  
  4. done
the part between do ... done is now yours.
read the man page for rev, and learn how to compare two strings in an if statement.
use one string for the original version of the $word variable, and reverse another.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:




Views: 9322 | Replies: 3
Thread Tools Search this Thread



Tag cloud for Shell Scripting
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC