Unix Shell - parsing dfs output for a system monitoring script.

This is Interesting: Free IT Magazines  
Home > Archive > Unix Shell > March 2006 > parsing dfs output for a system monitoring script.





You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

Author parsing dfs output for a system monitoring script.
TOC

2006-03-17, 2:57 am

I would like to change


Filesystem 1K-blocks Used Available Use% Mounted on
/dev/hda3 25159548 4997588 20161960 20% /
tmpfs 518224 0 518224 0% /dev/shm
/dev/hda1 97826 15809 76798 18% /boot


to:


/=20%;/dev/shm=0%;/boot=18%;



I made this mess to do it:

df | grep -v Filesystem | while read line; do set -- $line; echo -n
"$6"="$5"\;; done

can anyone think of a more elegant way to do it?


thanks


Ed Morton

2006-03-17, 2:57 am

TOC wrote:
> I would like to change
>
>
> Filesystem 1K-blocks Used Available Use% Mounted on
> /dev/hda3 25159548 4997588 20161960 20% /
> tmpfs 518224 0 518224 0% /dev/shm
> /dev/hda1 97826 15809 76798 18% /boot
>
>
> to:
>
>
> /=20%;/dev/shm=0%;/boot=18%;
>
>
>
> I made this mess to do it:
>
> df | grep -v Filesystem | while read line; do set -- $line; echo -n
> "$6"="$5"\;; done
>
> can anyone think of a more elegant way to do it?


df | awk 'NR>1{o=o$6"="$5";"}END{print o}'

Regards,

Ed.
Grant

2006-03-17, 2:57 am

On Fri, 17 Mar 2006 02:58:19 GMT, TOC <noone@nowhere.com> wrote:

>I would like to change
>
>
>Filesystem 1K-blocks Used Available Use% Mounted on
>/dev/hda3 25159548 4997588 20161960 20% /
>tmpfs 518224 0 518224 0% /dev/shm
>/dev/hda1 97826 15809 76798 18% /boot
>
>
>to:
>
>
>/=20%;/dev/shm=0%;/boot=18%;


Try: df | grep -v Filesystem | awk '{printf $6"="$5";"}'; echo

Grant.
--
Memory fault -- brain fried
Ed Morton

2006-03-17, 2:57 am

Grant wrote:
> On Fri, 17 Mar 2006 02:58:19 GMT, TOC <noone@nowhere.com> wrote:
>
>
>
>
> Try: df | grep -v Filesystem | awk '{printf $6"="$5";"}'; echo


You almost never need to use grep with awk. The above is exactly the
same as:

df | awk '!/Filesystem/{printf $6"="$5";"}'; echo

You can also add the final newline still within awk so you don't need to
call "echo" either:

df | awk '!/Filesystem/{printf $6"="$5";"}END{print ""}'

Regards,

Ed.
Grant

2006-03-17, 2:57 am

On Thu, 16 Mar 2006 22:33:32 -0600, Ed Morton <morton@lsupcaemnt.com> wrote:

>Grant wrote:
>
>You almost never need to use grep with awk. The above is exactly the
>same as:
>
> df | awk '!/Filesystem/{printf $6"="$5";"}'; echo
>
>You can also add the final newline still within awk so you don't need to
>call "echo" either:
>
> df | awk '!/Filesystem/{printf $6"="$5";"}END{print ""}'


Yeah, realised could've done it all in awk as soon as I saw your
other response, now there's a couple more for the '40 ways to
skin a cat' collection ;)

Grant.
--
Memory fault -- brain fried
Xicheng

2006-03-17, 2:57 am

TOC wrote:
> I would like to change
>
>
> Filesystem 1K-blocks Used Available Use% Mounted on
> /dev/hda3 25159548 4997588 20161960 20% /
> tmpfs 518224 0 518224 0% /dev/shm
> /dev/hda1 97826 15809 76798 18% /boot
>
>
> to:


a PERL solution:

perl -anl73e 'print "$F[5]=$F[4]" if $.>1' myfile.txt

Xicheng

>
>
> /=20%;/dev/shm=0%;/boot=18%;
>
>
>
> I made this mess to do it:
>
> df | grep -v Filesystem | while read line; do set -- $line; echo -n
> "$6"="$5"\;; done
>
> can anyone think of a more elegant way to do it?
>
>
> thanks


Xicheng

2006-03-17, 2:57 am

Xicheng wrote:
> TOC wrote:
>
> a PERL solution:
>
> PERL -anl73e 'print "$F[5]=$F[4]" if $.>1' myfile.txt
>
> Xicheng


Sorry that is not correct, one more ; shows up at the end of the
string(what a silly mistake ).. but a traditional PERL way might be:

df | PERL -ane 'push @x, "$F[5]=$F[4]" if $.>1 }{ print join ";", @x'

it doesnt look that good though..

Xicheng
[vbcol=seagreen]
>

Chris F.A. Johnson

2006-03-17, 2:57 am

On 2006-03-17, Ed Morton wrote:
> Grant wrote:
>
> You almost never need to use grep with awk.


You very rarely need to, but it is often faster than using awk on
its own.

> The above is exactly the
> same as:
>
> df | awk '!/Filesystem/{printf $6"="$5";"}'; echo
>
> You can also add the final newline still within awk so you don't need to
> call "echo" either:
>
> df | awk '!/Filesystem/{printf $6"="$5";"}END{print ""}'



--
Chris F.A. Johnson, author | <http://cfaj.freeshell.org>
Shell Scripting Recipes: | My code in this post, if any,
A Problem-Solution Approach | is released under the
2005, Apress | GNU General Public Licence
John W. Krahn

2006-03-17, 7:50 am

TOC wrote:
> I would like to change
>
> Filesystem 1K-blocks Used Available Use% Mounted on
> /dev/hda3 25159548 4997588 20161960 20% /
> tmpfs 518224 0 518224 0% /dev/shm
> /dev/hda1 97826 15809 76798 18% /boot
>
> to:
>
> /=20%;/dev/shm=0%;/boot=18%;
>
> I made this mess to do it:
>
> df | grep -v Filesystem | while read line; do set -- $line; echo -n
> "$6"="$5"\;; done
>
> can anyone think of a more elegant way to do it?


df | PERL -ne'/(\d+%)\s+(.+)/ && print "$2=$1", eof() ? "\n" : ";"'


John
--
use Perl;
program
fulfillment
John W. Krahn

2006-03-17, 7:50 am

TOC wrote:
> I would like to change
>
> Filesystem 1K-blocks Used Available Use% Mounted on
> /dev/hda3 25159548 4997588 20161960 20% /
> tmpfs 518224 0 518224 0% /dev/shm
> /dev/hda1 97826 15809 76798 18% /boot
>
> to:
>
> /=20%;/dev/shm=0%;/boot=18%;
>
> I made this mess to do it:
>
> df | grep -v Filesystem | while read line; do set -- $line; echo -n
> "$6"="$5"\;; done
>
> can anyone think of a more elegant way to do it?


df | PERL -ne'/(\d+%)\s+(.+)/ && print "$2=$1;", eof() && "\n"'


John
--
use Perl;
program
fulfillment
Michael Paoli

2006-03-17, 7:50 am

TOC wrote:
> I would like to change
> Filesystem 1K-blocks Used Available Use% Mounted on
> /dev/hda3 25159548 4997588 20161960 20% /
> tmpfs 518224 0 518224 0% /dev/shm
> /dev/hda1 97826 15809 76798 18% /boot
> to:
> /=20%;/dev/shm=0%;/boot=18%;
> I made this mess to do it:
> df | grep -v Filesystem | while read line; do set -- $line; echo -n
> "$6"="$5"\;; done
> can anyone think of a more elegant way to do it?


Not foolproof, but something like this is a bit more fool resistant:

$ pwd -P
/tmp
$ mkdir Filesystem filesystem 'foo bar' 'FOO BAR'
# mount --bind Filesystem filesystem
# mount --bind 'FOO BAR' 'foo bar'
$ df -k filesystem 'foo bar'
Filesystem 1K-blocks Used Available Use% Mounted on
/tmp/Filesystem 257824 1320 256504 1% /tmp/filesystem
/tmp/FOO BAR 257824 1320 256504 1% /tmp/foo bar
$ df -k filesystem 'foo bar' |
> PERL -e 'while(<> ){chomp;if(s/^.*(?:\s+\d+){3}\s+(\d+%) (.*)$/\2=\1;/){print;};};print "\n";'

/tmp/filesystem=1%;/tmp/foo bar=1%;
$
Similar logic could also be used with awk(1) or sed(1), but it would
be less elegant.

What happens to the handling of the (proposed) output format if mount
point(s) may contain ; and/or % characters?

http://www.rawbw.com/~mp/unix/sh/#G...mming_Practices

It's hard to make a program foolproof because fools are so ingenious.

If an exceedingly robust solution is required, one may want to start
with an examination of the source code for df(1).

Chris F.A. Johnson

2006-03-17, 5:54 pm

On 2006-03-17, TOC wrote:
> I would like to change
>
>
> Filesystem 1K-blocks Used Available Use% Mounted on
> /dev/hda3 25159548 4997588 20161960 20% /
> tmpfs 518224 0 518224 0% /dev/shm
> /dev/hda1 97826 15809 76798 18% /boot
>
>
> to:
>
>
> /=20%;/dev/shm=0%;/boot=18%;
>
>
>
> I made this mess to do it:
>
> df | grep -v Filesystem | while read line; do set -- $line; echo -n
> "$6"="$5"\;; done
>
> can anyone think of a more elegant way to do it?


df | { read ## read and ignore first line
while read fs bl u a pc mp
do
printf "%s=%s;" "$mp" "$pc"
done
printf "\n"
}

--
Chris F.A. Johnson, author | <http://cfaj.freeshell.org>
Shell Scripting Recipes: | My code in this post, if any,
A Problem-Solution Approach | is released under the
2005, Apress | GNU General Public Licence
bob the builder

2006-03-17, 5:54 pm

well if this was bash golf (shortest answer wins) then Ed got it... =)

well now that I think of it this is shorter:

df | awk '!/^F/{printf $6"="$5";"}';

and technically the OP (me) didn't need a trailing newline because this
is simply an entry in a csv log.

its really cool seeing how many ways you can skin a cat




df | grep -v Filesystem | while read line; do set -- $line; echo -n
"$6"="$5"\;; done;
df | awk 'NR>1{o=o$6"="$5";"}END{print o}'
df | grep -v Filesystem | awk '{printf $6"="$5";"}';
df | awk '!/Filesystem/{printf $6"="$5";"}';
df | awk '!/Filesystem/{printf $6"="$5";"}END{print ""}';
df | PERL -anl73e 'print "$F[5]=$F[4]" if $.>1';
df | PERL -ane 'push @x, "$F[5]=$F[4]" if $.>1 }{ print join ";", @x';
df | PERL -ne'/(\d+%)\s+(.+)/ && print "$2=$1", eof() ? "\n" : ";"'
df | PERL -ne'/(\d+%)\s+(.+)/ && print "$2=$1;", eof() && "\n"'
df | { read
while read fs bl u a pc mp
do
printf "%s=%s;" "$mp" "$pc"
done
printf "\n"
}

Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2008 webservertalk.com