Awk in ksh shell script - help


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Awk in ksh shell script - help
# 1  
Old 06-12-2008
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.
# 2  
Old 06-12-2008
use backquote(`)

shell_variable= `awk '$1 == "shell" {abc= $2 }' /tmp/cust_det`
echo $shell_variable
# 3  
Old 06-12-2008
one more question ..

Thank you for the correction Smilie

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
# 4  
Old 06-12-2008
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???
# 5  
Old 06-13-2008
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.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Use case insensitive variable in ksh shell scripting using sed or awk

I am using a variable called $variable in a pattern search to print from a starting variable to a constant value. the variable search should be case in sensitive. i tired using Ip at the end in the below command. but in ksh it is not working. sed -n "/$variable/,/constant/p" file i also... (11 Replies)
Discussion started by: johnjs
11 Replies

2. Shell Programming and Scripting

Help with ksh Shell Script

My goal is to create a script that will check if in a test or production environment. I wrote this script to check $host variable to check which server I'm on but this script does not work. if then BASE=/home/fmtest; export BASE else BASE=/home/fmprod; export BASE fi ... (5 Replies)
Discussion started by: Bperl1967
5 Replies

3. Shell Programming and Scripting

Help with KSH Shell Script

From a shell script I'm trying to remove the first two files of whats returned from the head -2 command so I tried piping it to xargs rm -f but I can't get it to work. How do I remove the files from the head command? ls -al *clit* *servr* |sort -t_ -nk2 | head -2 |xargs rm -f (3 Replies)
Discussion started by: Bperl1967
3 Replies

4. Shell Programming and Scripting

Help with ksh shell script

Anyone know how to check a filename that contains a date and compare whiich file is older using a ksh shell script? The filename looks like aaaaa_20110615 (1 Reply)
Discussion started by: Bperl1967
1 Replies

5. Shell Programming and Scripting

for loop in awk script using ksh

Guys, I am new in awk , I face problem while i try to use for loop in awk, I am using ksh, i am trying to set a for loop which runs as man times as the records in a file , the for loop like for(a=1;a<=5;a++) is working in my awk script but the one i need is not working :wall: for example ... (8 Replies)
Discussion started by: djahmed
8 Replies

6. Shell Programming and Scripting

Help with Backup Shell Script 'ksh + awk over ssh'

Hi newbeeeee alarm i want to send a little script over ssh this script mus download a report.tar then rename and move. the report name format is report_<host.with.dot>-10-09-20-11:55:25.tar function remote_cmd_mv { _host=$1 ARCHROOTDIR='/tmp' ... (8 Replies)
Discussion started by: TigerGoods
8 Replies

7. Shell Programming and Scripting

what does this ksh shell script do?

Can someone tell me when the script is called, what does it do? I can't see it is going to run anything. (1 Reply)
Discussion started by: dp100022
1 Replies

8. Shell Programming and Scripting

Help with ksh shell script

I am using /usr/bin/ksh in AIX I am reading the values of $dbname, $dbatmpdir/dbdir.$$, and $scope from a different file All I have to do is check if $dbname exists in file $dbatmpdir/dbdir.$$ and $scope should have a value either 'TABLE' or 'SCHEMA'. When I execute the following code. I am... (3 Replies)
Discussion started by: tenderfoot
3 Replies

9. Shell Programming and Scripting

need help with simple awk/ksh script

I need help finding out why this script wont run. The chmod is okay, but i get an error saying that I need '&&' on line 5. (18 Replies)
Discussion started by: tefflox
18 Replies

10. Shell Programming and Scripting

script ksh/awk/grep

How can I extract the 9th line of a text file. thanks vm.:confused: (2 Replies)
Discussion started by: hoang
2 Replies
Login or Register to Ask a Question