LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 02-16-2007, 12:32 AM   #1
sharathkv25
Member
 
Registered: Jul 2006
Distribution: HP-UX
Posts: 46

Rep: Reputation: 15
pass & retrieve variables from awk


Code:
#myvar.ks

VAR=6
echo "test" | awk '{ print '$VAR' }'
echo "test" | awk '{ '$VAR'=7 }'
echo 'Assign='$VAR
Code:
$ ./myvar.ks
6
 syntax error The source line is 1.
 The error context is
                { >>>  6= <<< 7 }
 awk: The statement cannot be correctly parsed.
 The source line is 1.
Assign=6
$
Does any one know how I can assign a value to a shell variable from AWK?

Thanks
 
Old 02-16-2007, 01:24 AM   #2
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 62
In awk, you can access variable which have been exported to the environment using the ENVIRON associative array. e.g.
Code:
$ export VAR="J. R. Bob Dobbs"
$ echo "Hi" | awk '{ print $1 " " ENVIRON["VAR"] }'
Hi J. R. Bob Dobbs
 
Old 02-16-2007, 02:29 AM   #3
sharathkv25
Member
 
Registered: Jul 2006
Distribution: HP-UX
Posts: 46

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by matthewg42
In awk, you can access variable which have been exported to the environment using the ENVIRON associative array. e.g.
Code:
$ export VAR="J. R. Bob Dobbs"
$ echo "Hi" | awk '{ print $1 " " ENVIRON["VAR"] }'
Hi J. R. Bob Dobbs
Hi,

I am able to access the shell variable in awk, as you can see in my my script posted above.

I need to know how to set a value to a shell variable from AWK, so I can use the variable in shell later on?

Thanks
 
Old 02-16-2007, 03:05 AM   #4
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 62
Quote:
Originally Posted by sharathkv25
Hi,

I am able to access the shell variable in awk, as you can see in my my script posted above.
Not really. When you do this:
Code:
$ VAR=test
$ awk '{ print '$VAR' }'
...you are executing the command:
Code:
awk '{ print test }'
Awk never sees the string "$VAR" - the shell pre-expands this before invoking awk - you have basically excluded it from the single quotes and allowed the shell to interpolate $VAR. That it is working is an illusion, which will break in many cases - e.g. your variable has a space or tab in it. For example:
Code:
$ VAR="my test"
$ awk '{ print '$VAR' }'
awk: cmd. line:1: { print my
awk: cmd. line:1:           ^ unexpected newline or end of string
Quote:
I need to know how to set a value to a shell variable from AWK, so I can use the variable in shell later on?

Thanks
You can't set something in the environment of a parent process. The environment variables are inherited by a child process from it's parent. If you write to the environment variables in a process, it will modify the copy in the memory space of that process, but cannot send it to the parent.

You can however take the standard output of a program, and assign that to a variable in the shell. This is a very different mechanism, but depending on what you want to do, it can have a similar effect:
Code:
$ var=$(echo "J. R. Bob Dobbs" |awk '{ print $3 }')
This will execute the command:
Code:
$ echo "J. R. Bob Dobbs" |awk '{ print $3 }'
...and assign the output of that command to the variable var. In this case, var will be "Bob".

A more general description is that $(command) is interpolated as the output of "command".
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
PHP script to retrieve records from MYSQL db and pass them to an external program Julianus Programming 3 08-03-2006 02:09 PM
pass variables through hyperlink in php ALInux Programming 2 12-23-2005 01:27 PM
pass value from awk back to bash shell cjs_pro Programming 2 03-07-2005 04:53 PM
awk: /matching/ variables passed with -v aunquarra Linux - General 2 02-17-2005 06:47 PM
script (or other) to pass variables to C++ program ngwenyama Programming 9 01-20-2005 09:54 PM


All times are GMT -5. The time now is 09:11 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration