Code Comments

Programming Forum and web based access to our favorite programming groups.
For Programmers: Free Programming Magazines | New: Database administration forum
Registration is free! Edit your profileCalendarFind other membersFrequently Asked QuestionsSearch -> 
Post New Thread











Thread
Author

Re: matching column variables from two awks!
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


Report this thread to moderator Post Follow-up to this message
Old Post
Rajan
03-24-08 03:00 AM


Re: matching column variables from two awks!
On 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.



Report this thread to moderator Post Follow-up to this message
Old Post
Ed Morton
03-24-08 03:00 AM


Re: matching column variables from two awks!
In 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 .


Report this thread to moderator Post Follow-up to this message
Old Post
Kenny McCormack
03-24-08 03:00 AM


Re: matching column variables from two awks!
Totally 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.
>
>

Report this thread to moderator Post Follow-up to this message
Old Post
Rajan
03-24-08 03:00 AM


Re: matching column variables from two awks!
On 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?

Report this thread to moderator Post Follow-up to this message
Old Post
spacegoose
03-24-08 11:59 PM


Re: matching column variables from two awks!

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 -


Report this thread to moderator Post Follow-up to this message
Old Post
Ed Morton
03-27-08 12:03 AM


Sponsored Links




Last Thread Next Thread Next
Pages (3): « 1 2 [3]
Search this forum -> 
Post New Thread

AWK archive

Show a Printable Version Send to friend Email This Page to Someone! subscribe to this thread Receive updates to this thread
Computer Consultants
Programming Jobs
Visual Basic Controls
SQL Server Programming
Webservices
Java Security
Visual Studio
C# Programming
Visual J++
Software engineering
Open source Software
Perl Programming
PHP Programming
ASP Programming
ASP .NET Programming
Visual Basic Programming
Windows Scripting Host
Java Programming
Java Help
Java Beans
VBScript
Cobol
MAC Applications
Unix Programming
Forum Jump:
All times are GMT. The time now is 03:37 PM.

 
Free MCSE Braindumps | Real Estate Topics

Programming forum archive

Copyrights CodeComments.com 2004 - 2006

Powered by vBulletin Copyright 2000-2006 Jelsoft Enterprises Limited.