On Fri, 13 Aug 2010 12:39:23 -0600, LuKreme wrote:
> On 12-Aug-2010, at 11:25, Michael Gersten wrote:
> >
> >>>
> >>> eval `vm_stat | sed -n '/^Pages /{;s///;s/: */=/;s/\.$//;s/[-  ]/_/g;p;}'`
> >>
> >> Oh, that's clever!
> >
> > Clever is understanding it .
>
> With all the lines that begin with "Pages " (/^Pages /) do the
> following: 0) do not echo the line (-n)
> {
> 1) nothing (;)
> 2) replace "Pages " with nothing (s///)
> 3) replace : and any number of spaces with an = (s/: */=/)
> 4) remove the period at the end of the line (s/\.$//
> 5) replace ever - or space with an underscore (s/[- ]/_/g)
> 6) print the result (p)
> }
> end

When I came up with this line of noise, I guess I was too lazy to  
remember whether or not a semicolon was required after {.  It's partly a  
matter of style, I think; if I were to make it a multi-liner I would write  
it as:

    /^Pages /{
        s///
        ...etc..
    }

so in my mind I just substituted the newlines with semicolons.

Maybe this is just too clever for its own good...  As Michael is using  
bash for his script, here is an alternative that is probably easier to  
follow, and uses bash itself to parse the vm_stat output (eliminating the  
sed altogether):

# Parse vm_stat output in bash.
# (NB. can't use pipe since variable assignments would then effectively
# happen in a subprocess, and not be seen by the remainder of the script.
# So use here-document (<<EOF) instead.)

while IFS=: read y z # split vm_stat output at ":"
do
  z=${z// /}    # strip all spaces (bash-ism)
  z=${z/./}     # strip "." (bash-ism)
  case "$y" in
  "Pages free") free=$z ;;
  "Pages active") active=$z ;;
  "Pages inactive") inactive=$z ;;
  esac
done <<EOF
`vm_stat`
EOF

--
Tom.
_______________________________________________
MacOSX-talk mailing list
[email protected]
http://www.omnigroup.com/mailman/listinfo/macosx-talk

Reply via email to