![]() |
|
|
|
|
|||||||
| Forums | Portal | Register | Forum Rules | FAQ | Contribute | Members List | Arcade | Search | Today's Posts | Mark Forums Read |
| Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here. |
|
|
||||
| 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 |
|
|
Submit Tools | LinkBack | Thread Tools | Display Modes |
|
|||
|
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. |
| Forum Sponsor | ||
|
|
|
|||
|
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
Code:
$1 == "shell" { print $2 }
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
|
|||
| Google The UNIX and Linux Forums |