Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

awk command in perl script quesion (unix)

Status
Not open for further replies.

kundog

Technical User
Apr 10, 2003
3
US

I have a question about putting an awk command into a perl script. I'm not a programmer by any means, and am just trying to make this work. I want to save the output for the result of:

/bin/df /images/vol8 | awk '{print $3}'

so, when i put it in the program, i want to do it like this:

$spacefree = `/bin/df /images/vol8 | awk '{print $3}'`

but i get errors on this b/c it chokes when it hits the single quotes from the awk statement. Anyone know how I can make perl ignore the single quotes from awk and just get the results of the command?
thanks a lot
 
Why on earth would you want to use awk from Perl????

This is not meant to be the fastest or best way to do it, but it would be better than running awk and hopefully will show you some useful things:



#!/usr/bin/perl
# collect the commands output in an array.
#
@p=`df -v`;
# now loop through the array
#
foreach (@p) {
# change multiple spaces to one. Note that's two spaces
# followed by an *
s/ */ /g;
# now split the line to another array
@f=split / /;
# and print what we want if the original line matches vol8
print $f[3] if /vol8/;
}

Also see Tony Lawrence
SCO Unix/Linux Resources tony@pcunix.com
 
I found a trick the other day - you can write that without the:

# change multiple spaces to one. Note that's two spaces
# followed by an *
s/ */ /g;


@p=`df -v`;
# now loop through the array
#
foreach (@p) {
# split the line to another array
@f=split;
# and print what we want if the original line matches vol8
print $f[3] if /vol8/;
}

split without arguments splits $_ on whitespace (\s+) and strips any leading whitspace..
Mike
"Experience is the comb that Nature gives us after we are bald."

Is that a haiku?
I never could get the hang
of writing those things.
 
Yes, I know that. I wanted to introduce him to split in general, and to have him notice that * means zero or more.

I dunno. I try not to write complicated code for examples, and I try to use examples that will demonstrate broader concepts rather than the way most experienced people would do it, but it probably is a pointless effort because if I take the time to explain everything the post is too long and if I don't I might as well have just posted an incomprehensible one-liner anyway.

In general, I appreciate step by step code when I'm introduced to new concepts, and very often I tend to write it that way even for my own internal use because it's easier to follow later when I come back to change it.

But as I said, it's probably pointless.
Tony Lawrence
SCO Unix/Linux Resources tony@pcunix.com
 
wasn't directed at you tony - I know you know that trick Mike
"Experience is the comb that Nature gives us after we are bald."

Is that a haiku?
I never could get the hang
of writing those things.
 
No, no, I wasn't complaining :)

I was just mumbling on about the frustrations of how to write example code. Tony Lawrence
SCO Unix/Linux Resources tony@pcunix.com
 
See if you've got a2p on your machine, if not get it.
write an awk script then run it through the a2p, and you get the perl equivelent. Theres also s2p

a2p = awk2perl
s2p = sed2perl

read the man pages

Mike [flush3] --
| Mike Nixon
| Unix Admin
| ----------------------------
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top