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.
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
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.
Method 2
@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.
Method 3
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}'
Method 4
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+=%sn'; echo s ) | bc
Method 5
With GNU find:
find -name '*.extract.sys' -size +1000000c -printf '%sn' | jq -s add
With GNU stat:
find . -name '*.extract.sys' -size +1000000c -exec stat -c%s '{}' + | jq -s add
With BSD stat:
find . -name '*.extract.sys' -size +1000000c -exec stat -f%z '{}' + | jq -s add
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0