The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
Google UNIX.COM


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
To call/execute a shell script from a shell script konark UNIX for Dummies Questions & Answers 1 10-26-2007 02:16 PM
How to pass a parameter from one Shell-script to another Shell-script subodhbansal Shell Programming and Scripting 2 09-22-2007 02:19 AM
How to Run a shell script from Perl script in Parent shell? hifake Shell Programming and Scripting 16 08-28-2007 05:42 PM
Accessing variables of one shell script in another shell script rsendhilmani Shell Programming and Scripting 1 04-30-2007 05:43 AM
Have a shell script call another shell script and exit heprox Shell Programming and Scripting 2 11-20-2006 04:17 AM

Reply
 
Submit Tools LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 06-12-2008
Registered User
 

Join Date: Jun 2008
Posts: 3
Stumble this Post!
Awk in ksh shell script - help

Hello,
Im a beginner. Im writing a ksh script with awk. Is it possible to assign the output of the awk to a shell variable?

Like,
shell_variable= awk '$1 == "shell" {abc= $2 }' /tmp/cust_det
echo $shell_variable

Please excuse my ignorance. Thanks in advance.
Reply With Quote
Forum Sponsor
  #2 (permalink)  
Old 06-12-2008
Registered User
 

Join Date: Apr 2008
Posts: 56
Stumble this Post!
use backquote(`)

shell_variable= `awk '$1 == "shell" {abc= $2 }' /tmp/cust_det`
echo $shell_variable
Reply With Quote
  #3 (permalink)  
Old 06-12-2008
Registered User
 

Join Date: Jun 2008
Posts: 3
Stumble this Post!
one more question ..

Thank you for the correction

How do i compare the shell argument say $1 with awk's $1 ?
or a shell variable with awk's $1 ??

shell_variable= `awk '$1 == "$shell" {abc= $2 }' test_file`
echo $shell_variable
Reply With Quote
  #4 (permalink)  
Old 06-12-2008
Registered User
 

Join Date: Jun 2008
Posts: 3
Stumble this Post!
I got something like this , it seems to work...

shell_variable= `awk '$1 == shell {print $2 } shell="$1" ' test`
echo $shell_variable

Any other better way???
Reply With Quote
  #5 (permalink)  
Old 06-13-2008
era era is offline
Herder of Useless Cats
 

Join Date: Mar 2008
Location: /there/is/only/bin/sh
Posts: 3,599
Stumble this Post!
If the goal is to display the output from the backticks, then the backticks and the echo are of course needless. But to actually capture the output from the awk script into a variable, that's how you do it. Now let's turn to how to mix shell and awk variables.

You can mix shell and awk by playing around with quotes. Most often, you need quoting in order to pass the awk script to awk as a single string (unless of course your script is so simple that it's a single token, from the shell's point of view) and conventionally, we use single quotes to prevent the shell from messing with the string's contents. But if you do want the shell to, say, place the value of its first argument inside the string, then you can do that by relaxing the quoting.

Code:
awk '$1 == "'"$1"'" { print $2 }' test
The tricky quoting there is a closing single quote, followed by $1 in double quotes, followed by an opening single quote. Inside the awk script, all of this is in another set of double quotes, to mark it as a string in awk. The "seesaw quoting" causes the shell to interpolate the value of $1 smack dab into the middle of the awk script. So if the value of the shell's variable $1 is "shell", then the awk interpreter will end up executing the following script:

Code:
$1 == "shell" { print $2 }
So the awk script is pieced together from three parts: $1 == " (in single quotes) followed by the value of the shell variable $1 (in double quotes), followed by " { print $2} (in single quotes).

Some newer awk interpreters allow you to avoid this tricky quoting by using the -v switch, but the original, ancient awk didn't have that.

Code:
awk -v var="$1" '$1 == var { print $2 }' test
Here, the awk variable var is assigned on the command line to the value of the shell variable $1.
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools
Display Modes




All times are GMT -7. The time now is 05:31 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008 The CEP Blog All Rights Reserved -Ad Management by RedTyger Visit The Global Fact Book

Content Relevant URLs by vBSEO 3.2.0