943,973 Members | Top Members by Rank

Ad:
Dec 8th, 2005
0

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

Expand Post »
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
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
darklord is offline Offline
2 posts
since Dec 2005
Dec 8th, 2005
0

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

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
Reputation Points: 62
Solved Threads: 10
Junior Poster
jim mcnamara is offline Offline
179 posts
since May 2004
Dec 9th, 2005
0

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

Quote 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?
Reputation Points: 10
Solved Threads: 0
Newbie Poster
darklord is offline Offline
2 posts
since Dec 2005
Dec 12th, 2005
0

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

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.
Reputation Points: 62
Solved Threads: 10
Junior Poster
jim mcnamara is offline Offline
179 posts
since May 2004

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Shell Scripting Forum Timeline: awk problem
Next Thread in Shell Scripting Forum Timeline: Anyone Could Help Me Out?? Please..





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC