help-gnu-utils
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: awk - Read file inside script.


From: Chris Jones
Subject: Re: awk - Read file inside script.
Date: Mon, 05 Jan 2009 20:52:37 -0500
User-agent: Mutt/1.5.13 (2006-08-11)

On Mon, Jan 05, 2009 at 12:14:25PM EST, Bob Proulx wrote:
> Chris Jones wrote:

[..]

> If you wish to be kind to people using random text editors then it
> would be nice to shorten those marker comment lines by at least one
> character so that they do not wrap around terminals.  Some editors put
> a line wrap indicator in the last column indicating that the line
> displayed is wrapped to the next line.

I normally use the default tw=72 that is set in my .vimrc ..  but, I was so
fussed up by my attempts to make this little script work and awk's sometimes
quirky syntax that I found myself constantly wrapping to the next line and
decided to set it manually to something larger so I could focus on the coding
never thinking I would later need to post it and ask for assistance .. 

And there it comes back to bite me .. 

> Also, as long as I am whining (:-) using eight columns for a single
> indention level is alot of whitespace!  Of course this is personal
> preference and a bike shed discussion but still...

Thanks, I have set tabstop.. etc. to 2 instead of 4 in my .vimrc..  

The reason it looked like I was using an indentation of 8 is that I had
removed some lines while trying to make it work: In particular I had deleted
the "while line" and naturally forgot to .. "disindent" what followed.

> > I have tried numerous things with while .. getfile .. restructuring the
> > above, adding a BEGIN section .. and gotten nowhere.
> 
> Try my modification of your program.  I used getline to read the file.
> 
> #!/usr/bin/gawk -f
> BEGIN {
>   while (getline < "/proc/meminfo") {
>     if ($1=="MemTotal:")    {mt = $2};      # mt = total ram on system
>     if ($1=="MemFree:")     {mf = $2};      # mf = free ram
>     if ($1=="Buffers:")     {mb = $2};      # mb = ram used for buffers
>     if ($1=="Cached:")      {mc = $2};      # mc = ram used for cache
>     if ($1=="SwapTotal:")   {st = $2};      # st = total swap
>     if ($1=="SwapFree:")    {sf = $2};      # sf = free swap
>   }
>   exit;
> }
> END {
>   pmu = (mt-(mf+mb+mc)) * 100 / mt;         # pmu = % of ram  used
>   psu = ((st-sf) * 100 / st);               # psu = % of swap used
>   printf ("%2.1f %s %2.1f %s\n"), pmu, "%", psu, "%";
> }

This is in essence identical to my first attempt .. but since I didn't save
earlier versions, I'll never know why mine was hanging .. looked like it was
waiting for some input from the keyboard, now I come to think of it.. should
have entered something/anything .. maybe it would have reacted and that might
have been enough to push me in the right direction.

The main differences that I can see now is that I was missing the commas at
the end of each line .. will have to check what they're for since awk wasn't
complaining .. and I also found some trailing spaces before the EOL.

And all the time, I was looking for a structural flaw.. something in awk's
processing logic that I misunderstood.. I should have looked more closely at
what vim's color syntax highlighting was telling me.. 

:-)

Thanks for your help!

CJ





reply via email to

[Prev in Thread] Current Thread [Next in Thread]