| | |
Scan text file to find all words of 4 characters or less
Please support our Shell Scripting advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Dec 2005
Posts: 2
Reputation:
Solved Threads: 0
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
•
•
Join Date: May 2004
Posts: 178
Reputation:
Solved Threads: 10
try awk. This is a start:
Shell Scripting Syntax (Toggle Plain Text)
awk '{ for(i=1;i<=NF;i++) { if(length($i)<5) {print $i} } }' filename > newfilename
•
•
Join Date: Dec 2005
Posts: 2
Reputation:
Solved Threads: 0
•
•
•
•
Originally Posted by jim mcnamara
try awk. This is a start:
Shell Scripting Syntax (Toggle Plain Text)
awk '{ for(i=1;i<=NF;i++) { if(length($i)<5) {print $i} } }' filename > newfilename
Taking this step further. How can I filter out these 4 or less characters words, so that the newfilename only contain palindromes words?
•
•
Join Date: May 2004
Posts: 178
Reputation:
Solved Threads: 10
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.
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.
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)
for word in `awk script above` do done
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.
![]() |
Similar Threads
- determining frequency of words in text file (Python)
- Replace text in a file (Shell Scripting)
- # of lines in a text file (Java)
- Need Help in Reading characters from a text file (C++)
- Inputting text file data into an array, please help! (C++)
Other Threads in the Shell Scripting Forum
- Previous Thread: awk problem
- Next Thread: Anyone Could Help Me Out?? Please..
Views: 9322 | Replies: 3
| Thread Tools | Search this Thread |
Tag cloud for Shell Scripting





