how to find number of words


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting how to find number of words
# 1  
Old 07-25-2009
how to find number of words

please
help me for this
"divide the file into multiple files containing no more than 50 lines each and find the number of words of length less than 5 characters"

Last edited by annapurna konga; 07-25-2009 at 09:08 AM..
# 2  
Old 07-25-2009
Is this a homework question?

Give an explanation of your real world problem and show us what you've gotten so far.
# 3  
Old 07-25-2009
use
split -l 100 myFile

will split the myFile into pieces with 100 line in each files.
File will be start with prefix xaa,xab etc
you can change the prefix

check man split
# 4  
Old 07-25-2009
Idea for basic construct to make the input data easier to deal with. We change the delimiter between words to a newline character with "tr" then process each word as one line. My example uses older shell techniques. If you have ksh95 you can easily find out the number of characters in a parameter without using "wc" or for that matter do integer arithmetic without using "expr". The "echo" with the "\c" is to echo without a newline which makes the character count in "wc" correct (in some shells you need to use "echo -n ...").
It always helps when posting on this forum to state which Operating System you have and which shell you prefer. Good luck.

Code:
COUNTER=0       # Count of words less than 5 characters
cat file.ext|tr ' ' '\n'|while read WORD
do
        SIZE=`echo "${WORD}\c"|wc -c`
        if [ ${SIZE} -lt 5 ]
        then
                COUNTER=`expr ${COUNTER} + 1`
        fi
        echo "${WORD}:size ${SIZE}"
done
echo "Words of less than 5 characters: ${COUNTER}"


Last edited by methyl; 07-25-2009 at 05:01 PM.. Reason: Various corrections
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How count the number of two words associated with the two words occurring in the file?

Hi , I need to count the number of errors associated with the two words occurring in the file. It's about counting the occurrences of the word "error" for where is the word "index.js". As such the command should look like. Please kindly help. I was trying: grep "error" log.txt | wc -l (1 Reply)
Discussion started by: jmarx
1 Replies

2. Shell Programming and Scripting

How to count words number for each paragraph?

Hi, I have file like this: >number1 dlejreoreltedgerere dgtrtryr >number2 dkfdjlgdjfeiflefjdfidlfjdifelfjefe fjdlfjdkfjdlfdjlfdjlfjdigeo gjelreoureofouererg >number4 dklfdjfoeueoruer fjeorueotueoirueorueorueore gjoeoueorueoreuroerueor joeuroerueorue How can i know how many words... (4 Replies)
Discussion started by: the_simpsons
4 Replies

3. Shell Programming and Scripting

How can I sort by n number is like words?

I want to sort a file with a list of words, in order of most occuring words to least occurring words as well as alphabetically. ex: file1: cat 3 cat 7 cat 1 dog 3 dog 5 dog 9 dog 1 ape 4 ape 2 I want the outcome to be: file1.sorted: dog 1 (12 Replies)
Discussion started by: castrojc
12 Replies

4. Shell Programming and Scripting

Count number of match words

Input: some random text SELECT TABLE1 some more random text some random text SELECT TABLE2 some more random text some random text SELECT TABLE3 some more random text some random text SELECT TABLE1 some more random text Output: 'SELECT TABLE1' 2 'SELECT TABLE2' 1 'SELECT TABLE3' 1 I... (5 Replies)
Discussion started by: chitech
5 Replies

5. Shell Programming and Scripting

print number of words in each line

Hi, Please suggest a way to print number of words in the end of each line. <input file> red aunt house blue sky bat and ball game <output file> red aunt house 3 blue sky 2 bat and ball game 4 Thanks! (2 Replies)
Discussion started by: mira
2 Replies

6. Shell Programming and Scripting

Shell script to find out words, replace them and count words

hello, i 'd like your help about a bash script which: 1. finds inside the html file (it is attached with my post) the code number of the Latest Stable Kernel, 2.finds the link which leads to the download location of the Latest Stable Kernel version, (the right link should lead to the file... (3 Replies)
Discussion started by: alex83
3 Replies

7. Shell Programming and Scripting

Split file by number of words

Dear all I am trying to divide a file using the number of words as a condition. Alternatively, I would at least like to be able to retrieve the first x words of a given file. Any tips? Thanks in advance. (7 Replies)
Discussion started by: aavv
7 Replies

8. UNIX for Dummies Questions & Answers

number of words in delimited string

Hi, I'm looking for one liner code for counting number of words in a delimited string.. I know about wc -w ..but if i give wc -w a.txt,b.txt it won't work with delimited sting as it was looking for files with a.txt and b.txt I know there will be a simple solution..i couldn't... (5 Replies)
Discussion started by: ammu
5 Replies

9. Shell Programming and Scripting

counts the number of distinct words

I'm looking to write a sample shell script that counts the number of distinct words in a text file given as Argument. Remark: White space characters are spaces, tabs, form feeds, and new lines. JUST with this commands tr, sort, grep. wc. Thanks. (14 Replies)
Discussion started by: Net-Man
14 Replies

10. Programming

how i display number in words

helo i want to implement the following concept in my project write a c/c++ algorithm for : accept a number from the user not greater than 6 digits and display the number in words i.e. if the input from the user is 18265 then the output should be Eighteen Thousand Two Hundred Sixty Five. if the... (3 Replies)
Discussion started by: amitpansuria
3 Replies
Login or Register to Ask a Question