![]() |
|
|
google unix.com
|
|||||||
| Forums | Register | Forum Rules | Links | Albums | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
| 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 |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
||||
|
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 |
|
||||
|
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 |
![]() |
| Bookmarks |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|