The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Dummies Questions & Answers
.
google unix.com



UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !!

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Problem using find and xargs quixote Shell Programming and Scripting 5 05-02-2008 10:24 PM
'find' and 'tar' combination kharen11 Shell Programming and Scripting 2 02-06-2008 08:17 AM
command usage on find with xargs and tar darkrainbow AIX 3 12-25-2007 06:25 PM
strange behavior of find with xargs jerardfjay Shell Programming and Scripting 9 08-09-2007 08:06 AM
find | xargs cat asal_email Shell Programming and Scripting 4 03-17-2005 12:16 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 07-08-2008
mr_bold mr_bold is offline
Registered User
  
 

Join Date: Jan 2007
Location: UB, Mongolia
Posts: 32
Combination of find -xargs & wc -l

Dear all,

I have to calculate sum of record count of files of the specified directory.
First I tried the following way which prints one or more outputs.
How can I sum of this output?

Code:
find /home/work/tmp/1/O/ -type f -print0 | xargs -0 wc -l | grep total
  1666288 total
  1073908 total
Thanks in advance
  #2 (permalink)  
Old 07-08-2008
zaxxon's Avatar
zaxxon zaxxon is offline Forum Staff  
Moderator
  
 

Join Date: Sep 2007
Location: Germany
Posts: 2,259
If it's just for that directory, you could do a wc -l on those files like
Code:
root@sremhv27:/usr/IBM/HTTPServer/conf/sicher> wc -l *| tail -1| cut -d " " -f1
     916 httpd.conf
     209 ldap.prop.elan
     209 ldap.prop.papyrus
    1334 total

# ie. 

root@sremhv27:/usr/IBM/HTTPServer/conf/sicher> wc -l *| tail -1 |awk '{print $1}'
1334
If you want to have it including files in the subdiretories (which might be the reason you are using find), I would go and just fill an array with it like:

Code:
#!/usr/bin/ksh

typeset -i RESULT=0

set -A ARRAY "`find . -type f -exec wc -l {} \; | awk '{print $1}'`"

for ELE in ${ARRAY[*]}; do
        let RESULT=${RESULT}+${ELE}
done

echo "Result is: ${RESULT}"
This ksh, but if you are using another shell, you can just do ARR=`somecommandtomakealist`; for ELE in $ARR; do something; done
So no extra defining of an array with set -A has to be done. Just wanted to do it correct for ksh.
  #3 (permalink)  
Old 07-08-2008
radoulov's Avatar
radoulov radoulov is offline Forum Staff  
addict
  
 

Join Date: Jan 2007
Location: Варна, България / Milano, Italia
Posts: 2,847
Assuming no newlines in the filenames:

Code:
(IFS=$'\n'; awk 'END{print NR}' $(find -type f))
If you have zsh:

Code:
awk 'END{print NR}' **/*(.)

Last edited by radoulov; 07-08-2008 at 04:16 AM.. Reason: corrected
  #4 (permalink)  
Old 07-08-2008
mr_bold mr_bold is offline
Registered User
  
 

Join Date: Jan 2007
Location: UB, Mongolia
Posts: 32
Thank you very much,

There aren't any subdirectory.
There are many files so I can't directly use wc or awk.

Code:
wc -l * | tail -1
-bash: /usr/bin/wc: Argument list too long
I can do it by following script in bash.

Code:
sum=0

for eachtotal in `find /home/work/tmp/1/O/ -maxdepth 1 -type f -print0 | xargs -0 wc -l | grep total | awk '{print $1}'`
do
sum=`expr $sum + $eachtotal`
done

echo $sum
Are there some way to do this in one line code, because I am writing bash shell script which consists of hundreds of lines?

  #5 (permalink)  
Old 07-08-2008
radoulov's Avatar
radoulov radoulov is offline Forum Staff  
addict
  
 

Join Date: Jan 2007
Location: Варна, България / Milano, Italia
Posts: 2,847
Something like this:

Code:
find -type f -exec wc -l {} +|awk '{_+=$1}END{print _}'
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

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 On




All times are GMT -4. The time now is 12:42 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0