Assigning nawk output to variables


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Assigning nawk output to variables
# 1  
Old 08-04-2005
Assigning nawk output to variables

I do a lot of command line scripting to capture data from files or other command output. I've checked in a number of Unix and scripting books but for the life of me I can't find out how to asign field data from nawk output into variables that I can manipulate later. For example, reading a two column file like /etc/hosts:

cat /etc/hosts | nawk '{print $1 "\t" $2}'

How can I get $1 and $2 into variables like $IP and $NAME?
# 2  
Old 08-04-2005
many ways.....

Code:
#!/bin/ksh

IP='1.1.1.1'
NAME='foo'

echo "BEFORE: IP->[${IP}] NAME->[${NAME}]"

echo '2.2.2.2 bar' | eval $(nawk '{printf("IP=%s NAME=%s\n", $1, $2)}')

echo "AFTER: IP->[${IP}] NAME->[${NAME}]"

# 3  
Old 08-16-2005
vgersh99,

Hmm, can't quite grasp the technique. I tried the following:

cat hosts | eval $(nawk '{printf("IP=%s NAME=%s\n", $1, $2)}') | echo $IP $NAME
AZPHNX-E-3600-02 ( got back 1/2 line of a 3500 line file)

cat hosts | eval $(nawk '{printf("IP=%s NAME=%s\n", $1, $2)}') | echo [${IP}] [${NAME}]
[] [AZPHNX-E-3600-02] (same results)


>for i in `cat hosts`
> do
> eval $(nawk '{printf("IP=%s NAME=%s\n", $1, $2)}')
> echo $IP $NAME
> done
^C (no output!)
# 4  
Old 08-16-2005
Quote:
Originally Posted by steveje0711
vgersh99,

Hmm, can't quite grasp the technique. I tried the following:

cat hosts | eval $(nawk '{printf("IP=%s NAME=%s\n", $1, $2)}') | echo $IP $NAME
AZPHNX-E-3600-02 ( got back 1/2 line of a 3500 line file)

cat hosts | eval $(nawk '{printf("IP=%s NAME=%s\n", $1, $2)}') | echo [${IP}] [${NAME}]
[] [AZPHNX-E-3600-02] (same results)
strange. what's your 'hosts' file look like?

Quote:
Originally Posted by steveje0711
>for i in `cat hosts`
> do
> eval $(nawk '{printf("IP=%s NAME=%s\n", $1, $2)}')
> echo $IP $NAME
> done
^C (no output!)
there's no input for this 'nawk' invokation.
you don't need awk anyway....

Code:
while read IP NAME junk
do
   echo "[${IP}] [${NAME}]"
done < /etc/hosts

# 5  
Old 08-18-2005
Though maybe not so pertinent to your particular problem at hand, but I think the basic principle you need to grasp here is that of command subsitution, implemented in the Bourne shell with backtics, `command`, or with newer Bourne derivatives also as $(command). Command subsitution replaces the output of the command at that point in the script. Thus if you want to save the output of a command as a shell variable for later use:
Code:
MY_VAR=$(my_command)

The <my_command> can be as fancy as you want -- it can be a huge pipeline of whatever.

If you want to save more than one variable from the output of a command, one option is to use MY_VAR=$(my_command), and then parse out $MY_VAR into its components, saving each in its own variable.
But maybe an easier method is to use the read builtin. Now, shell builtins are usually run as the same process as the invoking shell, and so variable assignments done by builtins will have effect in the current shell and its environment. However, in most Bourne-like shells, all commands in a pipeline will be executed as a subprocess -- even builtins and functions -- and so variable assignments done in pipelines will have no effect in the current shell's environment. (External commands are always run as subprocesses.)
I say this because it is tempting to try the following to save variable(s) from the output of a command:
Code:
mycomm | read VAR [VAR2]
# for example:
echo George Bush | read FIRST LAST
echo $FIRST
<nothing>
echo $LAST
<nothing>

Because read, though a shell builtin, is here part of a pipeline, and not a simple command, it will be executed as a subprocess. So VAR1 and VAR2 will still be undefined (or unchanged) in the current shell.
To get around this problem, the AT&T Korn shell -- but not pdksh, nor bash -- has been made different in this regard, because if a shell builtin or function is the last element of a pipeline, it will still be run in the current shell process, and so variable assignments will take effect in the current shell. Thus the above example will work in the AT&T ksh.
But in most other shells, one trick to get around this is by using here documents--rather than a pipe--to send the command's output into the standard input of read. For you can do command subsitution in a here doc. For example:
Code:
read FIRST LAST <<EOF
$(echo George Bush)
EOF
echo $FIRST
George
echo $LAST
Bush
# or using your original code--though it does not fit my discussion, for you
# probably need iteration if you want to get all pairs from /etc/hosts;
# but as a matter of principle of how to assign vars from command output
read IP NAME <<EOF
$(cat /etc/hosts | nawk '{print $1 "\t" $2}')
EOF

Note that bash and ksh93 (not ksh88) have a <<< name operator -- a "here string", really a one-line here document -- where name gets variable/command/arithmetic expanded. Thus in bash and ksh93 (maybe only some versions of ksh93) you can do:
Code:
read FIRST LAST <<< $(echo George Bush)

But it is sometimes useful to pipe stuff into read to set variables, but only if you want to use those variables transiently right there in that block of code, and not later in your script. For example, you can still do:
Code:
mycomm | while read MY_VAR; do
  other_commands_using $MY_VAR
done

I.e., $MY_VAR will be available in the while read compound statement, and you can use it in there. But it won't have effect in the current shell outside of this while read statement.
Note also that--in this regard--pipelines are not the same as file redirections, and so the read builtin in "read HOSTNAME < /etc/hostname" is NOT run in a subprocess, and $HOSTNAME will be accessible in the current shell. (We see this in the /etc/hosts example of this thread.) This is also the reason why the here document method outlined above works.

Last edited by hadarot; 08-21-2005 at 07:49 AM..
# 6  
Old 08-19-2005
...and by the way, if you do want to set a variable with a value from a subprocess, check out the thread at https://www.unix.com/shell-programming-and-scripting/20755-getting-value-variable-set-subprocess-script.html
# 7  
Old 08-19-2005
In Korn Shell you can use the set command:

spd01: /home/obd/tst > set $(echo george bush)
spd01: /home/obd/tst > print $1
george
spd01: /home/obd/tst > print $2
bush
spd01: /home/obd/tst >

after this
my_var1=$1
my_var2=$2

or use 'shift'.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Assigning Variables

Hi, Can the below be clarified please. i just want to know what is the difference between the two ways of assigning variables as mentioned below. export SRC_TBL=${SRC_TBL-"MMA_COPAY_PLN_FACT_STG"} export SRC_TBL="MMA_COPAY_PLN_FACT_STG" thanks in advance :) Arun (1 Reply)
Discussion started by: Arun Mishra
1 Replies

2. Shell Programming and Scripting

Assigning variables

i have variables RECIPIENTS_DEVL,RECIPIENTS_UACC,RECIPIENTS_PROD i have a case statement to get the phase variable: case ${WMD_UPHASE1} in u) WMD_UPHASE4=UACC;; i) WMD_UPHASE4=DEVL;; p) WMD_UPHASE4=PROD;; d) WMD_UPHASE4=DEVL;; *) WMD_UPHASE4=DEVL;; esac I am unable to... (3 Replies)
Discussion started by: Arun Mishra
3 Replies

3. Shell Programming and Scripting

Getting phone number, its message and assigning them into 2 variables then screen output.

Hi Everyone, I have a flatfile "inbox.txt" which contains some information: Location 0, folder "Inbox", SIM memory, Inbox folder SMS message SMSC number : "+24800000023" Sent : Sat 04 Aug 2012 09:01:00 PM +0700 Coding : Default GSM alphabet... (5 Replies)
Discussion started by: testcase
5 Replies

4. Shell Programming and Scripting

Need help in assigning output of n commands to n variables automatically inside a for loop

Please help me to automatically assign the output of awk command to the variables cs3, cs4, cs5 and cs6 using a for loop. The below code is not working. for i in 3 4 5 6 do cs$i=`awk -F"|" 'BEGIN{sum=0}{sum=sum+$'$i'}END{printf("%d\n", sum)}' css` done echo $cs3 $cs4 $cs5 $cs6 (9 Replies)
Discussion started by: thulasidharan2k
9 Replies

5. UNIX for Dummies Questions & Answers

Assigning variables using awk

Hi, I am having a line which is separated by - I need to extract each field and put into some variable. a=`echo "this-is-the-case" | awk -F- '{print $1}' ` b=`echo "this-is-the-case" | awk -F- '{print $2}' ` c=`echo "this-is-the-case" | awk -F- '{print $3}' ` d=`echo "this-is-the-case" | awk... (2 Replies)
Discussion started by: posix
2 Replies

6. UNIX for Advanced & Expert Users

assigning variables to their defaults

Hi, Is there any way to assign defaults values to the shell variables without reassigning them ( restarting the session) for example after login the value of ORACLE_HOME=/a/b/c i have changed this value from the console export ORACLE_HOME=/c/d now what if i want the value exported to... (1 Reply)
Discussion started by: clx
1 Replies

7. Shell Programming and Scripting

how to access values of awk/nawk variables outside the awk/nawk block?

i'm new to shell scripting and have a problem please help me in the script i have a nawk block which has a variable count nawk{ . . . count=count+1 print count } now i want to access the value of the count variable outside the awk block,like.. s=`expr count / m` (m is... (5 Replies)
Discussion started by: saniya
5 Replies

8. UNIX for Dummies Questions & Answers

assigning variables from standard output

What am I doing wrong? I was searching for the answer to assigning variables from output. I found this simple response ls -l apply_want.m | read perms links owner group size mtime1 mtime2 mtime3 file this should allow me to echo the variables echo "$perms | $links | $owner | $group |... (2 Replies)
Discussion started by: whamchaxed
2 Replies

9. Shell Programming and Scripting

assigning nawk output to shell variable

Hello friends, I doing the follwing script , but found problem to store it to a shell variable. #! /bin/sh for temp in `find ./dat/vector/ -name '*.file'` do echo $temp nawk -v temp=$temp 'BEGIN{ split(temp, a,"\/"); print a}' done output: ./dat/vector/drf_all_002.file... (6 Replies)
Discussion started by: user_prady
6 Replies

10. UNIX for Dummies Questions & Answers

assigning variables

Before I even attempt this, is it possible to grep for a pattern, maybe a partial sentence like "go to page 3", assign that to a variable and then use awk or something to pull out the 3 and assign it to a variable? So first I would have Gotopg = "go to page 3" then page = 3 (9 Replies)
Discussion started by: k@ssidy
9 Replies
Login or Register to Ask a Question