Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

This is the command I am using to list some files:

find . -name \*.extract.sys -size +1000000c -exec ls -lrt {} \;
-rw-r--r--   1 qa1wrk15   test       1265190 Sep 29 01:14 ./var/can/projs/ar/rep/extract/Sep/29/ar.ARAB-PI_7.20110929.extract.sys
-rw-r--r--   1 qa1wrk15   test       1345554 Sep 29 01:14 ./var/can/projs/ar/rep/extract/Sep/29/ar.ARAB-PI_2.20110929.extract.sys
-rw-r--r--   1 qa1wrk15   test       1370532 Sep 29 01:14 ./var/can/projs/ar/rep/extract/Sep/29/ar.ARAB-PI_3.20110929.extract.sys
-rw-r--r--   1 qa1wrk15   test       1399854 Sep 29 01:14 ./var/can/projs/ar/rep/extract/Sep/29/ar.ARAB-PI_8.20110929.extract.sys

and so on.

Now I want to calculate the total size of these files by summing up the 5th column. I thought of using awk, to do this so I tested the following in a particular directory

>ls -lrt | awk `{ print $1 }`
ksh: syntax error at line 1 : `{' unmatched

I don't understand what is the problem, why this syntax error.

I am thinking to try

ls -lrt | awk `BEGIN {total = 0} {for(i=0;i<NR;i++){total+=$5}} END {printf "%d",total} 

this also, but a simple awk script is not working.

Please suggest or correct me if I am wrong, or if there is a workaround for this.

share|improve this question
add comment

4 Answers

up vote 8 down vote accepted

First of all, you should use straight single quotes ('), not the inclined ones (`).

The awk inline script could be as follow:

ls -lrt | awk '{ total += $5 }; END { print total }'

so, no need to initialize total (awk initializes it to zero), and no need to loop, awk already executes the script on every line of input.

share|improve this answer
    
thanks a lot, @enzofib –  munish Sep 29 '11 at 7:07
add comment

@enzotib has already pointed out what your syntax error is - I'm going to go off on a little tangent.

Summing a column of numbers is one of those things that keeps popping up. I've ended up with this shell function:

sumcol() 
{ 
    awk "{sum+=\$$1} END {print sum}"
}

With this, your solution becomes:

ls -lrt | sumcol 5

That will sum the numbers in column 5 and print the value.

share|improve this answer
    
In effect, I was wondering why there is not a standard utility for that. –  enzotib Sep 29 '11 at 9:34
    
Goes right into my .aliases file. –  Konrad Rudolph Sep 29 '11 at 12:04
add comment

Here is another way to do this by using du:

find . -name \*.extract.sys -size +1000000c -print0 | du -c --files0-from=- | awk 'END{print $1}'
share|improve this answer
add comment

In case HP-UX find also has -printf option, that would be useful to compose a formula for bc to calculate:

( find . -name \*.extract.sys -size +1000000c -printf '%s+'; echo 0 ) | bc

If the formula gets too big, it can be calculated step-by-step:

( find . -name \*.extract.sys -size +1000000c -printf 's+=%s\n'; echo s ) | bc
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.