Linux Archive

Linux Archive (http://www.linux-archive.org/)
-   Debian User (http://www.linux-archive.org/debian-user/)
-   -   Setting Multiple Shell Variables from One Run of awk (http://www.linux-archive.org/debian-user/168707-setting-multiple-shell-variables-one-run-awk.html)

Martin McCormick 09-30-2008 06:48 PM

Setting Multiple Shell Variables from One Run of awk
 
Right now, I have a shell script that does the following:

hostname=`echo $NEWDEV |awk 'BEGIN{FS="."}{print $1}'`
domain=`echo $NEWDEV |awk 'BEGIN{FS="."}{print $2}'`
top0=`echo $NEWDEV |awk 'BEGIN{FS="."}{print $3}'`
top1=`echo $NEWDEV |awk 'BEGIN{FS="."}{print $4}'`

That looks inefficient (dumb) so I ask, is there a way
to assign the fields in an awk expression to shell variables as
one runs awk once?

Being able to do that would mean one run of awk instead
of the 4 shown here and, if file accesses are involved, there is
only one of those.

Thanks.

Martin McCormick WB5AGZ Stillwater, OK
Systems Engineer
OSU Information Technology Department Telecommunications Services Group


--
To UNSUBSCRIBE, email to debian-user-REQUEST@lists.debian.org
with a subject of "unsubscribe". Trouble? Contact listmaster@lists.debian.org

Ken Irving 09-30-2008 07:40 PM

Setting Multiple Shell Variables from One Run of awk
 
On Tue, Sep 30, 2008 at 01:48:46PM -0500, Martin McCormick wrote:
> Right now, I have a shell script that does the following:
>
> hostname=`echo $NEWDEV |awk 'BEGIN{FS="."}{print $1}'`
> domain=`echo $NEWDEV |awk 'BEGIN{FS="."}{print $2}'`
> top0=`echo $NEWDEV |awk 'BEGIN{FS="."}{print $3}'`
> top1=`echo $NEWDEV |awk 'BEGIN{FS="."}{print $4}'`
>
> That looks inefficient (dumb) so I ask, is there a way
> to assign the fields in an awk expression to shell variables as
> one runs awk once?

>From awk(1):

OPTIONS
-v var=val
--assign var=val
Assign the value val to the variable var, before
execution of the program begins. Such variable values
are available to the BEGIN block of an AWK program.

So maybe try:

hostname=$(awk -v FS=$NEWDEV '{print $1}')

> Being able to do that would mean one run of awk instead
> of the 4 shown here and, if file accesses are involved, there is
> only one of those.

Ok, the above doesn't address that issue, but maybe you could set a
shell array with several values in one operation. If you write a
shell script to set the variables, maybe you could then source that
script so that the changes are made to the current shell.

Ken

--
Ken Irving, fnkci@uaf.edu, 907-474-6152
Water and Environmental Research Center
Institute of Northern Engineering
University of Alaska, Fairbanks


--
To UNSUBSCRIBE, email to debian-user-REQUEST@lists.debian.org
with a subject of "unsubscribe". Trouble? Contact listmaster@lists.debian.org

Bob McGowan 09-30-2008 07:48 PM

Setting Multiple Shell Variables from One Run of awk
 
Martin McCormick wrote:

Right now, I have a shell script that does the following:

hostname=`echo $NEWDEV |awk 'BEGIN{FS="."}{print $1}'`
domain=`echo $NEWDEV |awk 'BEGIN{FS="."}{print $2}'`
top0=`echo $NEWDEV |awk 'BEGIN{FS="."}{print $3}'`
top1=`echo $NEWDEV |awk 'BEGIN{FS="."}{print $4}'`

That looks inefficient (dumb) so I ask, is there a way
to assign the fields in an awk expression to shell variables as
one runs awk once?

Being able to do that would mean one run of awk instead
of the 4 shown here and, if file accesses are involved, there is
only one of those.

Thanks.

Martin McCormick WB5AGZ Stillwater, OK
Systems Engineer

OSU Information Technology Department Telecommunications Services Group




Yes, but it takes both awk and shell to do it. And, because the awk
script has a bunch of stuff in it that the shell recognizes, this works
best if you can put the script in a file. FYI, I tested this, cut and
past to a file and the command line, it worked for me (of course, I had
to use some bogus but properly formatted input ;).


The file would look like this:

BEGIN{FS="."}
{print "hostname=" $1";domain=" $2";top0=" $3";top1=" $4}

The shell part is this:

eval $(echo $NEWDEV | awk -f awkscriptname)

I prefer the $(...) to the grave accent form, it's easier to read. But
if you need backwards compatibility with a shell that doesn't support
the new syntax, use the back ticks.


--
Bob McGowan


All times are GMT. The time now is 01:46 PM.

VBulletin, Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.