Word count files in UNIX


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Word count files in UNIX
# 1  
Old 10-07-2014
Word count files in UNIX

My files:
file1 and file2 contain 157 words.
Emptyfile1 contain 0 words.

My script has to output how many files is filled with words and how many that are not.

If there are files that have words in it. It has to output "file2 and file2 contains 157 words, 0 files are empty"
If not every file is empty the output has to be "file1 and file2 contains 157 and emptyfile1, 1 file is empty"

I'm doing something wrong, can't seem to find the problem.


Code:
#!/bin/bash

while [ "$#" -gt 0 ]
do 
   if [ -s "$1" ]
   then
       wc -w "$1" "$2" "$3"
   else
        echo "there are $# that contain zero words/bytes"
   fi
   shift
done


Last edited by vbe; 10-07-2014 at 12:01 PM..
# 2  
Old 10-07-2014
Can you comment your script and explain what it should do ( or you expected it to do) so we can after, correct you?
# 3  
Old 10-07-2014
yes i will
# 4  
Old 10-07-2014
I ment what was you expecting in $# ? in $1, $2, $3 ? Or where does $# get its value etc...
# 5  
Old 10-07-2014
$1 $2 $3 are parameters. Value: file1 file2 file3
$#: its to switch from $1 to $2 and $3 with shift. it redoes the script again for each parameter.

I got 3 files. 2 are filled with words and 1 is empty.

If there are files that have words in it. It has to output "file2 and file2 contains 157 words, 0 files are empty"
If not every file is empty the output has to be "file1 and file2 contains 157 and emptyfile1, 1 file is empty"
# 6  
Old 10-07-2014
OK
$1 $2 $3 are positional parameters ( not mentioning $#) so you are to give them in input, but I see nowhere in your script where you read them...
# 7  
Old 10-07-2014
Can you help me out, i'm a beginner. So its a bit difficult for me
How can i read them?
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

UNIX script to check word count of each word in file

I am trying to figure out to find word count of each word from my file sample file hi how are you hi are you ok sample out put hi 1 how 1 are 1 you 1 hi 1 are 1 you 1 ok 1 wc -l filename is not helping , i think we will have to split the lines and count and then print and also... (4 Replies)
Discussion started by: mirwasim
4 Replies

2. Shell Programming and Scripting

Word count in html files

Hi does somebody know how to do a word count in a .html file? Just the text words, without all the html code. Thanks (4 Replies)
Discussion started by: louisJ
4 Replies

3. UNIX for Dummies Questions & Answers

Find EXACT word in files, just the word: no prefix, no suffix, no 'similar', just the word

I have a file that has the words I want to find in other files (but lets say I just want to find my words in a single file). Those words are IDs, so if my word is ZZZ4, outputs like aaZZZ4, ZZZ4bb, aaZZZ4bb, ZZ4, ZZZ, ZyZ4, ZZZ4.8 (or anything like that) WON'T BE USEFUL. I need the whole word... (6 Replies)
Discussion started by: chicchan
6 Replies

4. Shell Programming and Scripting

total count of a word in the files

Hi Friends, Need help regarding counting the word "friend" in files test1.txt and test2.txt. ( there is no gap/space between word ) cat test1.txt himynameisrajandiamfriendofrajeshfriend wouldyouliketobemyfriend. cat test2.txt himynameisdostandiamfriendofdostfriend... (2 Replies)
Discussion started by: forroughuse
2 Replies

5. UNIX for Dummies Questions & Answers

Separate text files in a folder by word count

Hi, been searching Google for like an hour and I doubt I got the right keywords so might as well ask here. What I need: Before: Article1.txt 564 Article2.txt 799 Article3.txt 349 Article4.txt 452 * Separate files over 400 wordcount * After: Article1.txt 564... (3 Replies)
Discussion started by: Richard2000
3 Replies

6. Shell Programming and Scripting

~~Unix command to count a particular word in the whole directory .~~

Hi , i'm trying to count a particular word occurance in a whole directory..is this possible :wall: say for example there is a directory with 100 files which and all the file may have the word 'aaa' in it ...how would i count the number of 'aaa' in those whole 100 files in a directory ? ... (10 Replies)
Discussion started by: Rabbitsfoot
10 Replies

7. Shell Programming and Scripting

Need script to count specific word and iterate over number of files.

Hi Experts, I want to know the count of specific word in a file. I have almost 600+ files. So I want to loop thru each file and get the count of the specific word. Please help me on achieving this... Many thanks (2 Replies)
Discussion started by: elamurugu
2 Replies

8. Shell Programming and Scripting

Word count of lines ending with certain word

Hi all, I am trying to write a command that can help me count the number of lines in the /etc/passwd file ending in bash. I have read through other threads but am yet to find one indicating how to locate a specifc word at the end of a line. I know i will need to use the wc command but when i... (8 Replies)
Discussion started by: warlock129
8 Replies

9. UNIX for Dummies Questions & Answers

word count ascii files

how do I display the total number of words in a file which is of the type ascii text (1 Reply)
Discussion started by: madtim
1 Replies
Login or Register to Ask a Question