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?
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:
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:
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:
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:
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:
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.
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)
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)
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)
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)
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)
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)
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)
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)
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)
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)