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/168765-setting-multiple-shell-variables-one-run-awk.html)

Bob McGowan 09-30-2008 08:52 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

"Mumia W.." 09-30-2008 09:21 PM

Setting Multiple Shell Variables from One Run of awk
 
On 09/30/2008 01:48 PM, 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




The bash shell can do this internally since it supports arrays:

NEWDEV=${NEWDEV//./ }
NEWDEV=($NEWDEV)
echo "hostname: ${NEWDEV[0]}"
echo "domain: ${NEWDEV[1]}"
echo "top0: ${NEWDEV[2]}"
echo "top1: ${NEWDEV[3]}"

Of course you're free to assign the array elements to variables if you
desire. See "man bash" for more about arrays.




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

Bob McGowan 10-01-2008 03:47 PM

Setting Multiple Shell Variables from One Run of awk
 
Mumia W.. wrote:

On 09/30/2008 01:48 PM, Martin McCormick wrote:




<--deleted problem description and awk solution-->



The bash shell can do this internally since it supports arrays:

NEWDEV=${NEWDEV//./ }
NEWDEV=($NEWDEV)
echo "hostname: ${NEWDEV[0]}"
echo "domain: ${NEWDEV[1]}"
echo "top0: ${NEWDEV[2]}"
echo "top1: ${NEWDEV[3]}"

Of course you're free to assign the array elements to variables if you
desire. See "man bash" for more about arrays.




This is certainly true. There's almost always another way to solve a
problem.


The only issue I would have with this solution is that it's specific to
modern shells, such as bash and ksh. If you want something that will
also work with older, less feature full shells, you may not be able to
use such advanced features.


Otherwise, a nice solution, as it avoids the extra process for awk.

--
Bob McGowan

Tzafrir Cohen 10-01-2008 04:14 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}'`

eval can be used to set more than one variable in a single command. This
is used by e.g. ssh-agent.

# Using a separate function for slightly more clarity
domain_parser() {
echo $1 | sed -e 's/^([^.]*).([^.]*).([^.]*).([^.]*)$/hostname="1" ; domain="2"; top0="3"; top1="4"/'
}
eval `domain_parser $NEWDEV`


But then, why do you assume a domain name has exactly four elements?

--
Tzafrir Cohen | tzafrir@jabber.org | VIM is
http://tzafrir.org.il | | a Mutt's
tzafrir@cohens.org.il | | best
ICQ# 16849754 | | friend


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


All times are GMT. The time now is 07:35 AM.

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