Re: Setting Multiple Shell Variables from One Run of awk
- From: "Mumia W.." <paduille.4061.mumia.w+nospam@xxxxxxxxxxxxx>
- Date: Tue, 30 Sep 2008 16:21:30 -0500
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@xxxxxxxxxxxxxxxx with a subject of "unsubscribe". Trouble? Contact listmaster@xxxxxxxxxxxxxxxx
- References:
- Setting Multiple Shell Variables from One Run of awk
- From: Martin McCormick
- Setting Multiple Shell Variables from One Run of awk
- Prev by Date: Re: how to create an image of your debian computer hard drive for cloning
- Next by Date: Re: how to create an image of your debian computer hard drive for cloning
- Previous by thread: Re: Setting Multiple Shell Variables from One Run of awk
- Index(es):
Relevant Pages
|