awk help needed

Giles Orr gilesorr-Re5JQEeQqe8AvxtiuMwx3w at public.gmane.org
Sun Jul 19 19:53:12 UTC 2009


2009/7/19 Jamon Camisso <jamon.camisso-H217xnMUJC0sA/PxXw9srA at public.gmane.org>:
> Chris F.A. Johnson wrote:
>>
>> On Sun, 19 Jul 2009, Jamon Camisso wrote:
>>
>>> Giles Orr wrote:
>>>>
>>>> 2009/7/18 Alex Beamish <talexb-Re5JQEeQqe8AvxtiuMwx3w at public.gmane.org>:
>>>>>
>>>>> On Sat, Jul 18, 2009 at 11:46 AM, Giles Orr<gilesorr-Re5JQEeQqe8AvxtiuMwx3w at public.gmane.org> wrote:
>>>>>>
>>>>>> By way of introduction: I'm finally, finally trying to get a new
>>>>>> edition of the Bashprompt HOWTO out there.  This will probably result
>>>>>> in me posting a lot of detailed and mildly weird questions to this
>>>>>> list.  This is the first.
>>>>>>
>>>>>> The intention of this script is to figure out how much space the files
>>>>>> in the current directory take up.  There are about a million ways to
>>>>>> do this - and yes, I know that "ls -l" spits out a "total" line: I
>>>>>> don't know what it's totaling, but my math has never agreed with it
>>>>>> ... feel free to explain though.  I decided that I'd like to do this
>>>>>> as much in awk as possible since it does decimal math (unlike bash)
>>>>>> and it's certainly the easiest way to do the text parsing.  I've tried
>>>>>> bc as well, but you have to use other utility programs to parse and
>>>>>> split the input for it.
>>>>>
>>>>> Sorry, but I usually use the du command for this.
>>>>>
>>>>>  root at music:/etc/init.d# du -h .
>>>>>  504K    .
>>>>>
>>>>> Sometimes I want to know how heavy an entire tree is, so I use
>>>>>
>>>>>  $ du -sh foo
>>>>>
>>>>> The 'h' argument does intelligent size management, so shows K, M and G.
>>>>
>>>> I would love to use "du" because initially it seems like precisely the
>>>> right tool ... but I want only the sum of the sizes of the files in
>>>> the current directory, and "du" is by default recursive (which also
>>>> makes it painfully slow to return, not a good thing for something
>>>> incorporated into a Bash prompt).  If it's possible to stop "du" from
>>>> recursing, I'll use it immediately - but that looks difficult to
>>>> impossible.  Any thoughts?
>>>>
>>>> Thanks to everyone else who answered too: it all helped.  It certainly
>>>> sounds like piping into "awk" is the way to go rather than trying to
>>>> write a self-contained "awk" script.
>>>>
>>>
>>> Try this:
>>
>>  DON'T.
>>
>>> #!/bin/bash
>>>
>>> i=0 j=0 k=0
>>> echo -n "Enter directory: "
>>> read dir
>>> for i in `ls $dir`
>>
>>    Not only is ls unnecessary, but it will cause the script to fail
>>    if any filenames contain spaces or other pathological characters.
>>    Use:
>>
>> for i in "$dir"/*
>>
>>>  do
>>>   if [ -f $i ]; then
>>>     j=`du -s $i |awk '{print $1}'`
>>>     k=`expr $k + $j`
>>>   fi
>>>  done
>>> echo $k
>>
>>
>
> Interesting, I started with for i in * but that barfs on stuff too. Suppose
> since I don't have an files I can see with spaces in their names
> (pathological hatred for such names), it appears to work correctly for me :)

Rewritten in bash rather than sh (this is for the Bash Prompt HOWTO, I
can assume they're using bash), removing the use of awk, and targeting
just the current directory:

#!/bin/bash
# filename "lsbytesum.du"

filesize=0
total=0
for filename in ./*
 do
   if [ -f "${filename}" ]
   then
     duout=$(du -s "${filename}")
     #filesize=$(du -s "${filename}" |awk '{print $1}')
     total=$((${total} + ${duout%       *}))
   fi
 done
echo ${total}

A note about "${duout%       *}" - "du" uses a tab as a separator, so
that's what the whitespace is here.

And to clarify the intended use:

$ export PS1="[\w (\$(lsbytesum.du)KiB)]\n[\D{%Y%m%d.%H%M}]\$ "
[/home/music/Supertramp/Breakfast in America (43388KiB)]
[20090719.1542]$ cd ..
[/home/music/Supertramp (0KiB)]
[20090719.1543]$ cd /iso/
[/iso (8593564KiB)]
[20090719.1545]$

I used a music folder because, while I agree with Jamon to a certain
extent about spaces in names, I do like them in music files ...  We
won't discuss how much owning that album dates me.

I'll work on the display (ie. KiB, MiB, GiB ...) and confirm that in
fact it _is_ KiB rather than KB, but this is almost exactly what I was
looking for.  Thanks!

-- 
Giles
http://www.gilesorr.com/
gilesorr-Re5JQEeQqe8AvxtiuMwx3w at public.gmane.org
--
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