awk help needed
Mike Kallies
mike.kallies-Re5JQEeQqe8AvxtiuMwx3w at public.gmane.org
Sat Jul 18 16:33:06 UTC 2009
Giles Orr wrote:
...
> I know I can do this sort of thing:
>
> ls -l | awk '/^-/ {total+=$5} ; END {printf ("%.0f", total) }'
>
> But I'd like to have a fully self-contained awk script. Any suggestions?
>
Bash is better at including awk than awk is at including pipes and
redirection.
e.g,
mike at Ubuntu-VMWare:~$ cat ./thing.bash
#!/bin/bash
ls -l | awk '
BEGIN {
total=0
}
/^.*-/ {
total+=$5
}
END {
while ( ( total/=1024 ) > 1 )
magnitude++
if ( magnitude == 0 ) scalesize = "B"
if ( magnitude == 1 ) scalesize = "KiB"
if ( magnitude == 2 ) scalesize = "MiB"
if ( magnitude == 3 ) scalesize = "GiB"
if ( magnitude == 4 ) scalesize = "TiB"
if ( magnitude == 5 ) scalesize = "PiB"
if ( magnitude == 6 ) scalesize = "EiB"
if ( magnitude == 7 ) scalesize = "ZiB"
if ( magnitude == 8 ) scalesize = "YiB"
printf ("%.2f%s\n", (total*1024), scalesize)
}
'
mike at Ubuntu-VMWare:~$ ./thing.bash
34.49KiB
It's self-contained, but the downside is that you lose the syntax
highlighting of your text editor. To get around that, I sometimes
troubleshoot in a secondary awk file, then include the whole thing when
I'm done.
(I heard that there *is* a way to do this kind of self-contained stuff
exclusively in awk, but the syntax is weird and I could never make it
work. )
-Mike
--
The Toronto Linux Users Group. Meetings: http://gtalug.org/
TLUG requests: Linux topics, No HTML, wrap text below 80 columns
How to UNSUBSCRIBE: http://gtalug.org/wiki/Mailing_lists
More information about the Legacy
mailing list