Re: 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@xxxxxxx, 907-474-6152
Water and Environmental Research Center
Institute of Northern Engineering
University of Alaska, Fairbanks


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



Relevant Pages

  • Re: Using getline in AWK in a Shell function to read from standard input
    ... while ("ls -1" | getline) print ... The above isn't an appropriate application for awk as awk is just ... being used to invoke shell commands which is precisely what shell is ... poster to make the code comparable. ...
    (comp.lang.awk)
  • Re: Using getline in AWK in a Shell function to read from standard input
    ... while ("ls -1" | getline) print ... The above isn't an appropriate application for awk as awk is just ... being used to invoke shell commands which is precisely what shell is ... poster to make the code comparable. ...
    (comp.lang.awk)
  • Re: Using getline in AWK in a Shell function to read from standard input
    ... no more data for the inherent awk loop it receives an EOF from ...   print ++n, $0} ... being used to invoke shell commands which is precisely what shell is ... shell scripting, too, when he saw how nice it was. ...
    (comp.lang.awk)
  • Re: Insert column different number
    ... first case and the awk program terse... ... (But this shell pieces would be a bit, I suppose, even considering ... it would be interesting to see how the data files are being processed in the rest of shell script. ...
    (comp.lang.awk)
  • Re: Pulling out fields in a CSV file
    ... You don't need head and tail, awk can do the line selection: ... <temp read HOSTNAME IP MODEL NOTUSED SITE BUILDING ROOM ... (You might be able to use a pipe depending on whether your shell puts ...
    (comp.lang.awk)