Computing.Net > Forums > Unix > Awk in a script

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Awk in a script

Reply to Message Icon

Name: Carlos Picardal
Date: March 2, 2004 at 09:23:35 Pacific
OS: AIX 5.0
CPU/Ram: ??
Comment:

When running the script below I get nothing..
I am a newbie with this. Not sure If you can run awk in a script. Also Im having problems getting a variable from the command line to the script.

from command line;
./getnode IQSB

script
#!/bin/ksh

db=$0

awk '
/Database name =/ {
db_found = ($4 == "$db" ? 1 : 0);
}
/Node name =/ && db_found {
print $4
}
' listdb.txt



Sponsored Link
Ads by Google

Response Number 1
Name: aigles
Date: March 2, 2004 at 10:51:59 Pacific
Reply:

The first parameter is $1 and not $0 ($0 is the script name).

The substitution $db is not done in the awk script :

solution 1)
db_found = ($4 == "'$db'" ? 1 : 0);

solution 2)
Use awk variable:
awk -v DB="$db" '
. . .
db_found = ($4 == DB ? 1 : 0);



#!/bin/ksh

db=$1

awk -v DB="$db" '
/Database name =/ {
   db_found = ($4 == DB);
}
/Node name =/ && db_found {
   print $4
}
' listdb.txt

Jean-Pierre.


0
Reply to Message Icon

Related Posts

See More


Parameter file for a shel... Awk search / /



Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Unix Forum Home


Sponsored links

Ads by Google


Results for: Awk in a script

awk in shell script www.computing.net/answers/unix/awk-in-shell-script/6200.html

AWK in a while loop www.computing.net/answers/unix/awk-in-a-while-loop/6669.html

deleting rows in a file www.computing.net/answers/unix/deleting-rows-in-a-file/3676.html