Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
google site



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

Reply
English Japanese Spanish French German Portuguese Italian Powered by Powered by Google
 
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
Registered User
 

Join Date: Dec 2009
Posts: 660
Thanks: 6
Thanked 49 Times in 47 Posts
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-14-2010
thillai_selvan's Avatar
Registered User
 

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

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..
  #5  
Old 03-14-2010
karthigayan's Avatar
Registered User
 

Join Date: Apr 2009
Location: /usr/bin
Posts: 116
Thanks: 0
Thanked 2 Times in 2 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
Reply

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


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 07:36 AM.