954,525 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

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

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

darklord
Newbie Poster
2 posts since Dec 2005
Reputation Points: 10
Solved Threads: 0
 

try awk. This is a start:

awk '{ 
       for(i=1;i<=NF;i++) 
       {
         if(length($i)<5) 
         {print $i}
       }
       }'  filename  > newfilename
jim mcnamara
Junior Poster
180 posts since May 2004
Reputation Points: 62
Solved Threads: 10
 

try awk. This is a start:

awk '{ 
       for(i=1;i<=NF;i++) 
       {
         if(length($i)<5) 
         {print $i}
       }
       }'  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?

darklord
Newbie Poster
2 posts since Dec 2005
Reputation Points: 10
Solved Threads: 0
 

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.

for word in `awk script above`
do

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.

jim mcnamara
Junior Poster
180 posts since May 2004
Reputation Points: 62
Solved Threads: 10
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You