count words and empty files


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting count words and empty files
# 1  
Old 03-14-2010
count words and empty files

Hello,

I will count words in a file (or more files) and count (if given up) empty files (test -z?), how can I do this? Like this:
There are "108" words in "3" files
There are "2" empty files

Thanks for your reaction.

Regards,

Arjan Engbers

(My English is not good, I hope you understand me)
# 2  
Old 03-14-2010
Hi, arjanengbers:

To count the number of words in each file in the current directory (including empty files):
Code:
wc -w *

Regards,
Alister
# 3  
Old 03-15-2010
Code:
find ~ -empty -type f | wc -l

Use the above command for finding the empty files count in the home directory(~)
You can use some specific path for finding the empty files under that path.
# 4  
Old 03-15-2010
Code:
for i in $@
do
        if [ ! -s $i ]
        then
                echo "$i is empty";
        fi
    echo `wc -w $i`;
done


Last edited by murugaperumal; 03-15-2010 at 01:52 AM..
# 5  
Old 03-15-2010
Solution

try the following ,it will give you the exact output,

Code:
j=0
k=0
count=0
total=0
for i in $@
do
        if [ -s $i ]
        then
                count=`wc -w $i|cut -d ' ' -f 1`
                let total=$count+$total
                let k=$k+1
        else
                let j=$j+1
        fi
done
echo "There are \"$total\" words in $k files"
echo "There are \"$j\" empty files"

Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Programming

Count columns that are non-empty per line

The file is similar to the attached. Thousands of columns, delimiter is tab, with many columns containing free text and space separated. I want to get the count of columns with non-empty entries. eg.Col1=10, Col6=5, Col8=1 awk preferred (7 Replies)
Discussion started by: genehunter
7 Replies

2. Shell Programming and Scripting

Count the words starting with 3-

OS : Oracle Linux 6.5 Shell : bash I have a file whose contents look like below. I want to count the number of occurences of strings starting with 3-. How can I do this ? I couldn't wordwrap the below line. Hence it looks long. '3-90892405251', '3-90892911050', '3-90893144163',... (8 Replies)
Discussion started by: John K
8 Replies

3. Shell Programming and Scripting

Error files count while coping files from source to destination locaton as well count success full

hi All, Any one answer my requirement. I have source location src_dir="/home/oracle/arun/IRMS-CM" My Target location dest_dir="/home/oracle/arun/LiveLink/IRMS-CM/$dc/$pc/$ct" my source text files check with below example.text file content $fn "\t" $dc "\t" $pc "\t" ... (3 Replies)
Discussion started by: sravanreddy
3 Replies

4. Shell Programming and Scripting

Count words from file

hi all how to count words from a text aaa bbb ccc ddd 123 aaa 123 aaa aaa ddd 123 i need to cout hoe many time the words "aaa" and "123" each appears the output should be 4 3 or 4 3 or aaa 4 123 3 thanks (10 Replies)
Discussion started by: sharong
10 Replies

5. 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

6. Shell Programming and Scripting

How to count the number of occurrence of words from multiple files?

File 1 aaa bbb ccc File 2 aaa xxx zzz bbb File 3 aaa bbb xxx Output: (4 Replies)
Discussion started by: Misa-Misa
4 Replies

7. 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

8. Shell Programming and Scripting

Count words

Hi, does anyone know the command to count words by its length. I need to Count the number of five letter words that I have in a file with thousand of words. thanks (3 Replies)
Discussion started by: fabioamaury
3 Replies

9. Shell Programming and Scripting

Count the no of lines between two words

Please help in the following problem: Input is: Pritam 123 456 Patil myname youname Pritam myproject thisproject iclic Patil remaining text some more text I need the command which will display the no of lines between two words in the whole file. e.g. Display all the no of lines... (5 Replies)
Discussion started by: zsudarshan
5 Replies

10. UNIX for Dummies Questions & Answers

Count of Field for Non-Empty

Hi Guys, I wanted to count the number of records for a particular field of a file. whose fields are separated by comma"," I fI use this command. cat "filename" cut -sd "," -f13 | wc -l This shows all the lines count including the blank values for the field number 13. I wanted to count... (2 Replies)
Discussion started by: Swapna173
2 Replies
Login or Register to Ask a Question