Can I use wc -l with arithmetic expression?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Can I use wc -l with arithmetic expression?
# 1  
Old 08-11-2007
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  
Old 08-11-2007
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  
Old 08-11-2007
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  
Old 08-11-2007
A far more elegant solution by reborg. Quite nice.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Need help with date arithmetic please

Hello fellow forum members, I wrote below piece of code to calculate the date after a given date - date=$DATE_FINAL declare -a max_month=(0 31 28 31 30 31 30 31 31 30 31 30 31) eval $(echo $date|sed 's!\(....\)\(..\)\(..\)!year=\1;month=\2;day=\3!') (( year4=year%4 )) (( year100=year%100... (9 Replies)
Discussion started by: ektubbe
9 Replies

2. Shell Programming and Scripting

Why Relational Expression is Writing to a Expression?

Hello All, Not sure why this is happening... When the following If Statement is evaluated for some reason it is creating a file in the CWD called '0'. I've seen this happen before, just not in an If Statement... CODE: if then DIR_NAME="$1" DIR_SIZE=0 STATUS="" else... (3 Replies)
Discussion started by: mrm5102
3 Replies

3. UNIX for Advanced & Expert Users

sed: -e expression #1, char 0: no previous regular expression

Hello All, I'm trying to extract the lines between two consecutive elements of an array from a file. My array looks like: problem_arr=(PRS111 PRS213 PRS234) j=0 while } ] do k=`expr $j + 1` sed -n "/${problem_arr}/,/${problem_arr}/p" problemid.txt ---some operation goes... (11 Replies)
Discussion started by: InduInduIndu
11 Replies

4. Programming

Perl: How to read from a file, do regular expression and then replace the found regular expression

Hi all, How am I read a file, find the match regular expression and overwrite to the same files. open DESTINATION_FILE, "<tmptravl.dat" or die "tmptravl.dat"; open NEW_DESTINATION_FILE, ">new_tmptravl.dat" or die "new_tmptravl.dat"; while (<DESTINATION_FILE>) { # print... (1 Reply)
Discussion started by: jessy83
1 Replies

5. Shell Programming and Scripting

Integer expression expected: with regular expression

CA_RELEASE has a value of 6. I need to check if that this is a numeric value. if not error. source $CA_VERSION_DATA if * ] then echo "CA_RELESE $CA_RELEASE is invalid" exit -1 fi + source /etc/ncgl/ca_version_data ++ CA_PRODUCT_ID=samxts ++ CA_RELEASE=6 ++ CA_WEEK_NO=7 ++... (3 Replies)
Discussion started by: ketkee1985
3 Replies

6. Programming

Pointer Arithmetic In C

I have a fundamental question on C pointer arithmetry.. Suppose i have a c string pointer already pointing to a valid location, Can I just do a charptr = charptr +1; to get to the next location, irregardless if my program is 32 or 64 bits? or should i do it this way: charptr =... (1 Reply)
Discussion started by: Leion
1 Replies

7. UNIX for Dummies Questions & Answers

Arithmetic: how to??

Hello all, I'd like to know how to perform arithmetic on multiple files. I have got many tab-delimited files. Each file contains about 2000 rows and 2000 columns. What I want to do is to to sum the values in each row & column in every file. The following explains what I want to do; ... (9 Replies)
Discussion started by: Muhammad Rahiz
9 Replies

8. Shell Programming and Scripting

Need help with arithmetic expression using variables

Hi, I am trying to do a percentage calculation in a Bourne shell script. The following works fine, but when I try to subsitute the values 153824 and 246000 for variables (e.g. used and limit), I get errors. calc() { awk 'BEGIN {OFMT="%f"; print '"$*"'; exit}' } calc '(153824 /... (3 Replies)
Discussion started by: shorehil
3 Replies

9. Programming

error: initializer expression list treated as compound expression

I had seen this error for the first time ..... error: initializer expression list treated as compound expression please help.... (12 Replies)
Discussion started by: arunchaudhary19
12 Replies

10. Shell Programming and Scripting

Regular Expression + Aritmetical Expression

Is it possible to combine a regular expression with a aritmetical expression? For example, taking a 8-numbers caracter sequece and casting each output of a grep, comparing to a constant. THX! (2 Replies)
Discussion started by: Z0mby
2 Replies
Login or Register to Ask a Question