The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
Google UNIX.COM


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

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
count number of files in a directory finalight Shell Programming and Scripting 3 05-21-2008 09:14 PM
trying to count lines in multiple files llsmr777 UNIX for Dummies Questions & Answers 1 01-23-2008 10:52 AM
Count the number of files in a directory Raynon Shell Programming and Scripting 14 08-16-2007 07:07 PM
awk command to find the count of files ina directory sish78 Shell Programming and Scripting 11 07-19-2007 05:00 AM
How to find the count of files in a directory sish78 UNIX for Dummies Questions & Answers 5 02-01-2007 12:58 PM

Reply
 
Submit Tools LinkBack Thread Tools Search this Thread Display Modes
  #1  
Old 03-10-2006
Registered User
 

Join Date: Mar 2006
Posts: 8
Count files lines in a directory?

Hy!

I have some problem. Problem is that i don't now how to solve problem of average lines of files in a directory.

I have managed to get number of files in a directory, but i don't know the command to count average lines of these files.

I have one "for" loop that goes true whole directory...

Thanks in advance!
Reply With Quote
Forum Sponsor
  #2  
Old 03-10-2006
vgersh99's Avatar
Moderator
 

Join Date: Feb 2005
Location: Boston, MA
Posts: 3,016
look into 'man wc'
Reply With Quote
  #3  
Old 03-10-2006
RTM's Avatar
RTM RTM is offline
Hog Hunter
 
Join Date: Apr 2002
Location: On my motorcycle
Posts: 3,039
Use wc command to get the number of lines (if I remember correctly, wc adds one or subtracts one from the actual count). Add the total number, divided by number of files for the average number of lines.
Reply With Quote
  #4  
Old 03-10-2006
Registered User
 

Join Date: Mar 2006
Posts: 8
Yes but i don't know how to use wc command in a for loop on all files. I tried butt i have not managed to make it.

Example: I have 5 files in a directory. And i want to count their average lines.

@RTM - your idea is good but i'm newbie in linux. I will be very gratefoul if you guys can explain a little bit more.
Reply With Quote
  #5  
Old 03-10-2006
vgersh99's Avatar
Moderator
 

Join Date: Feb 2005
Location: Boston, MA
Posts: 3,016
to count all '.txt' files:
Code:
#!/bin/ksh

typeset -i sum=0
typeset -i cnt=0

for i in *.txt
do
   sum=$(( sum + $(wc -l < "${i}") ))
   cnt=$(( cnt + 1 ))
done

printf "sum->[%d] cnt->[%d] avgSZ->[%.2f]\n" ${sum} ${cnt} "$(echo "scale=2; $sum / $cnt" | bc)"
Reply With Quote
  #6  
Old 03-12-2006
Registered User
 

Join Date: Jul 2005
Posts: 137
Code:
ruby -e 'c=ARGV.size;p ARGF.to_a.size/c.to_f' *
This won't work if the total number of lines is too large, since all of the lines will be put into an array.
Reply With Quote
  #7  
Old 03-12-2006
Registered User
 

Join Date: Mar 2006
Posts: 8
Yes but i need that code in linux shell script. I'm only beginning shell scripting i don't now much about it...Thanx
Reply With Quote
Google The UNIX and Linux Forums
Reply

Tags
linux

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes




All times are GMT -7. The time now is 10:37 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008. All Rights Reserved.Ad Management by RedTyger Visit The Complex Event Processing Blog

Content Relevant URLs by vBSEO 3.2.0