Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
Search Forums:



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 03-14-2010
Registered User
 

Join Date: Mar 2010
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
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)
Sponsored Links
    #2  
Old 03-14-2010
alister alister is offline Forum Advisor  
Registered User
 

Join Date: Dec 2009
Posts: 1,496
Thanks: 39
Thanked 308 Times in 270 Posts
Hi, arjanengbers:

To count the number of words in each file in the current directory (including empty files):

Code:
wc -w *

Regards,
Alister
Sponsored Links
    #3  
Old 03-14-2010
thillai_selvan's Avatar
Registered User
 

Join Date: Feb 2010
Location: Chennai
Posts: 190
Thanks: 0
Thanked 1 Time in 1 Post

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-14-2010
murugaperumal's Avatar
Registered User
 

Join Date: Feb 2010
Location: Chennai
Posts: 91
Thanks: 0
Thanked 0 Times in 0 Posts

Code:
for i in $@
do
        if [ ! -s $i ]
        then
                echo "$i is empty";
        fi
    echo `wc -w $i`;
done


Last edited by murugaperumal; 03-14-2010 at 11:52 PM..
Sponsored Links
    #5  
Old 03-14-2010
karthigayan's Avatar
Registered User
 

Join Date: Apr 2009
Location: India , Chennai
Posts: 137
Thanks: 0
Thanked 3 Times in 3 Posts
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"

Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
perl: count columns/words in a string hcclnoodles Shell Programming and Scripting 2 02-18-2010 09:28 AM
Count the no of lines between two words zsudarshan Shell Programming and Scripting 5 09-17-2009 10:01 AM
count no of words in a line Satyak Shell Programming and Scripting 3 09-26-2008 11:52 AM
Count of Field for Non-Empty Swapna173 UNIX for Dummies Questions & Answers 2 08-08-2008 09:48 AM
count no of words in a line satish@123 Shell Programming and Scripting 7 05-21-2008 02:59 AM



All times are GMT -4. The time now is 03:49 AM.