Code Comments
Programming Forum and web based access to our favorite programming groups.you can do this with a combination of sort and paste but if you are a gawky
like me
BEGIN{
cmd="df -h | grep whatever | sort -n +5"
while(cmd | getline)
{ mntspace[$1]=$2;mntused[$1]=$3}
cmd="awk {'print $5'} myFile |sort |uniq -c |sort -nr"
while(cmd|getline)
{mntacs[$2]=$1}
printf "%-12s %-12s %-12s %-12s", "Disk", "Space", "Used", "Accounts"
for (each1 in mntspace) {printf "%-12s %-12s %-12s %-12s", each1,
mntspace[each1], mntused[each1], mntacs[each1]}
exit
}
"spacegoose" <spacegoose@gmail.com> wrote in message
news:8acca503-5eaf-4d95-abca-c8fd6390f99c@o22g2000hsh.googlegroups.com...
> i have a program that prints out a formatted df command
> by sorting and grepping and using awk to print it out.
>
> it basically executes df -h | grep whatever | sort -n +5 and then
> awks
> to a nice printout like:
>
> disk size capacity
> ------------------
> foo 20gb 70%
> moo 40gb 25%
> bar 20gb 70%
>
> i'd like to add a new column "accounts" to this print out.
>
> the no. of accounts is derived from a command:
> awk {'print $5'} myFile |sort |uniq -c |sort -nr
>
> which prints rows like:
>
> 450 foo
> 300 moo
> 104 bar
>
> i want to integrate this command into the formatted df program's
> output.
> i can't figure out how to appropriately append the "account count"
> column from the myFile command, to the appropriate row of the
> formatted df output,
> e.g. where col 1 from the formatted df output matches col 2 of the
> myFile command.
>
> so it looks like this:
>
>
> disk size capacity accounts
> ---------------------------
> foo 20gb 70% 450
> moo 40gb 25% 300
> bar 20gb 70% 104
>
>
> Thanks for any hints!
> sg
Post Follow-up to this messageOn 3/23/2008 4:04 PM, Rajan wrote:
[please don't top-post, fixed below]
> "spacegoose" <spacegoose@gmail.com> wrote in message
> news:8acca503-5eaf-4d95-abca-c8fd6390f99c@o22g2000hsh.googlegroups.com...
>
>
> you can do this with a combination of sort and paste but if you are a gawk
y
> like me
>
> BEGIN{
> cmd="df -h | grep whatever | sort -n +5"
> while(cmd | getline)
> { mntspace[$1]=$2;mntused[$1]=$3}
> cmd="awk {'print $5'} myFile |sort |uniq -c |sort -nr"
> while(cmd|getline)
> {mntacs[$2]=$1}
> printf "%-12s %-12s %-12s %-12s", "Disk", "Space", "Used", "Accounts"
> for (each1 in mntspace) {printf "%-12s %-12s %-12s %-12s", each1,
> mntspace[each1], mntused[each1], mntacs[each1]}
> exit
> }
>
I don't mean to be rude, but the above is missing the point of awk as a
text-processing tool and trying to force it to behave like a shell, i.e. an
environment for invoking tools. It's totally the wrong approach to solving t
his
or any other problem.
Ed.
Post Follow-up to this messageIn article <47E6D0B5.4010608@lsupcaemnt.com>, Ed Morton <morton@lsupcaemnt.com> wrote: ... >I don't mean to be rude, but the above is missing the point of awk as a >text-processing tool and trying to force it to behave like a shell, i.e. an >environment for invoking tools. It's totally the wrong approach to solving this >or any other problem. > > Ed. > > That's what makes it so.
Post Follow-up to this messageTotally agreed. I'm being misunderstood, this is definitely not about the filter this is just about matching the multiple columns. I should have mentioned that in my post! Rajan "Ed Morton" <morton@lsupcaemnt.com> wrote in message news:47E6D0B5.4010608@lsupcaemnt.com... > On 3/23/2008 4:04 PM, Rajan wrote: > > [please don't top-post, fixed below] > > > I don't mean to be rude, but the above is missing the point of awk as a > text-processing tool and trying to force it to behave like a shell, i.e. > an > environment for invoking tools. It's totally the wrong approach to solving > this > or any other problem. > > Ed. > >
Post Follow-up to this messageOn Mar 23, 1:41 am, Ed Morton <mor...@lsupcaemnt.com> wrote:
> On 3/23/2008 12:13 AM, spacegoose wrote:
> <snip>
>
>
> I think what you want is to put this in a file:
>
> ------------
> df -h | sort +5 | awk '
> BEGIN {
> printf "%-15s%12s%10s%10s%10s%10s\n", "File System", "bytes", "used", "ava
il",
> "capacity", "accounts"
> print "-------------------------------------------------------------------
"}
>
> NR==FNR{ accts["/local/ds/"$5]++; next }
> /dsk/ { printf "%-15s%12s%10s%10s%10s%10s\n", $6, $2, $3, $4, $5, accts[$6
]+0 }
> ' myFile -
> -------------
>
> Regards,
>
> Ed.
This is working, Thanks Ed.
Can you make the columns tally at the bottom?
Post Follow-up to this message
On 3/24/2008 10:55 AM, spacegoose wrote:
> On Mar 23, 1:41 am, Ed Morton <mor...@lsupcaemnt.com> wrote:
>
>
>
> This is working, Thanks Ed.
> Can you make the columns tally at the bottom?
df -h | sort +5 | awk '
BEGIN {
printf "%-15s%12s%10s%10s%10s%10s\n", "File System", "bytes", "used", "avail
",
"capacity", "accounts"
print "-------------------------------------------------------------------"
}
NR==FNR{ accts["/local/ds/"$5]++; next }
/dsk/ {
printf "%-15s%12s%10s%10s%10s%10s\n", $6, $2, $3, $4, $5, accts[$6]+0
tot["bytes"]+=$2
tot["used"]+=$3
tot["avail"]+=$4
tot["capacity"]+=$5
tot["accounts"]+=accts[$6]
}
END {
printf "%-15s%12s%10s%10s%10s%10s\n", "Totals", tot["bytes"], tot["used"],
tot["avail"], tot["capacity"], tot["accounts"]
}
' myFile -
Post Follow-up to this messagePowered by vBulletin
Copyright 2000-2006 Jelsoft Enterprises Limited.