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

Recommended Answers

All 3 Replies

try awk. This is a start:

awk '{ 
       for(i=1;i<=NF;i++) 
       {
         if(length($i)<5) 
         {print $i}
       }
       }'  filename  > newfilename

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?

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.