[Gllug] Small shell script challenge
Tim Towers
tt at createservices.com
Tue Dec 16 18:01:25 UTC 2003
To utilise your original fragment you can wrap an expr and sed round it
as follows...
expr `echo \`ls -lR . | awk '{ print $5 }'\` | sed -e 's/ / + /g'
The easier awk version...
ls -lR . | awk '{ count += $5 } ; END { print count }'
Tim
-----Original Message-----
From: gllug-bounces at gllug.org.uk [mailto:gllug-bounces at gllug.org.uk] On
Behalf Of Tethys
Sent: 16 December 2003 17:09
To: Greater London Linux Users Group
Subject: Re: [Gllug] Small shell script challenge
Richard Jones writes:
>Using something like:
>
>ls -lR dir | awk '{print $5}' | ....
>
>I can get a list of numbers (actually including some blank lines).
>What's the quickest way to add up those numbers?
As Alain has said, just use awk to total stuff up for you. But if you
want to do it from a shell script, there are several ways. For example:
ls -lR dir | awk '{print $5}' | \
while read a; do [ ! -z "$a" ] && tot=$(($tot + $a)); echo "$tot";
done | \
tail -1
Alternatively:
ls -lR dir | awk '{print $5}' | sed 's/$/ +p/' | dc 2>/dev/null |
tail -1
Or slightly differently:
(ls -lR tmp/ | awk '{print $5}' | sed 's/$/ +/'; echo p) | dc
2>/dev/null
Tet
--
Gllug mailing list - Gllug at gllug.org.uk
http://lists.gllug.org.uk/mailman/listinfo/gllug
--
Gllug mailing list - Gllug at gllug.org.uk
http://lists.gllug.org.uk/mailman/listinfo/gllug
More information about the GLLUG
mailing list