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 and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
error: initializer expression list treated as compound expression arunchaudhary19 High Level Programming 12 11-16-2007 06:44 AM
arithmetic in ksh amon Shell Programming and Scripting 12 02-05-2007 02:43 AM
Arithmetic In UNIX tygun UNIX for Dummies Questions & Answers 3 11-23-2005 05:46 PM
arithmetic syntax paprbagprincess UNIX for Dummies Questions & Answers 1 05-11-2004 09:30 AM
Regular Expression + Aritmetical Expression Z0mby Shell Programming and Scripting 2 05-21-2002 11:59 AM

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

Join Date: Aug 2007
Posts: 43
Can I use wc -l with arithmetic expression?

Folks,

I am wondering that i can use something like this in one line.
For example, $((cat filename > wc -l) / 2)
It doesn't work; how to get it work using command substitution?

Moreover, is there any option for wc -l not to return filename after the line counts?
wc -l filename would return, for instance, 10 filename

I want the line counts only.

Thanks a lot
  #2 (permalink)  
Old 08-11-2007
hegemaro hegemaro is offline
Registered User
  
 

Join Date: Feb 2006
Location: Schenectady, NY
Posts: 134
You can use sed to strip away the file name. For example, this will sum up the line count for an entire directory and save it as $I.

I=0

for LINES in $(wc -l * | sed -e "s/^ *\([0-9]*\) .*/\1/")
do
(( I += LINES ))
done

echo $I


The tr and cut commands could also be used as in

... for LINE in $(wc -l * | tr -s " " "\t" | cut -f2) ...

But for very large files, the line count could extend to the left-most column and you'd end up cutting the wrong field.

Your example would be written as:

expr $(wc -l filename | sed -e "s/^ *\([0-9]*\) .*$/\1/") / 2
  #3 (permalink)  
Old 08-11-2007
reborg's Avatar
reborg reborg is offline Forum Staff  
Administrator
  
 

Join Date: Mar 2005
Location: Ireland
Posts: 4,246
Quote:
Moreover, is there any option for wc -l not to return filename after the line counts?

Code:
wc -l < filename



Code:
(( count = $(wc -l < filename) / 2 ))

  #4 (permalink)  
Old 08-11-2007
hegemaro hegemaro is offline
Registered User
  
 

Join Date: Feb 2006
Location: Schenectady, NY
Posts: 134
A far more elegant solution by reborg. Quite nice.
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 10:43 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