Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Matching Variable inside Pattern 1

Status
Not open for further replies.

JJ1

Programmer
Apr 19, 2001
104
GB
All,

I'm passing variables to AWK using the -v option (as recommended in the man page).

This seems to work, but not when the variable is placed inside a regexp for matching.

This is my script's input:
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
root 3098 0.0 0.0 1388 444 tty5 S Jul08 0:00 /sbin/mingetty tty5
root 3099 0.0 0.0 1388 444 tty6 S Jul08 0:00 /sbin/mingetty tty6
myuser1 3332 0.0 0.4 50196 17716 ? S Sep03 0:00 rvrd -listen tcp:localhost:xxxx -http xxxx -no-permanent -reliability
myuser1 3747 0.0 0.8 100048 35448 ? S Sep03 0:00 rvrd -listen tcp:localhost:xxxx -http xxxx -permanent -reliability 30

AWK is being called from a bash shell script:

#Username is like 'root'

$NAWK -v ScriptPid="$$" -v UserName="$USERNAME" '{
print "Looking for:", UserName "

#This condition never matches for some reason:
if ( ($1 ~ /UserName/) && ($2 !~ /^[ ]*ScriptPid[ ]*$/ ) {
FoundProcess = "YES"
print "A process is still running as", UserName ":", $1 ",", $2 ",", $11 "\n"
}
else {
print "Not true for this line:"
print $0
}

}
END {
if( FoundProcess != "YES" ) {
exit 0
}
else {
exit 1
}
}' << EOF
`/path/to/ps`
EOF

Unfortunately, the code never matches the if statement.

Does anyone know how I can solve this one please?

Thanks in advance,

James.
 
AWK doesn't expect variables in REs (how would it know the difference between a variable name and a string?).

Try adding a BEGIN{} clause and set UserNameRE="/" UserName "/", then change your if statement to if ( ($1 ~ UserNameRE) && ....

Annihilannic.
 
Replace this;
if ( ($1 ~ /UserName/) && ($2 !~ /^[ ]*ScriptPid[ ]*$/ ) {
By this:
if ( ($1 ~ UserName) && ($2 !~ "^[ ]*"ScriptPid"[ ]*$" ) {


Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
Thank you all for the reply.

I like PHV's answer best and replaced my original if statement with this:


if ( ($1 ~ "^"UserName"") && ($2 !~ "^[ ]*"ScriptPid"[ ]*$") ) {


So, presumably, one must quote the awk variables and also delimit their regexp's with quotes as well?

Much appreciated.
Regards,

James.
 
[tt]if ( ($1 ~ "^"UserName"") && ($2 !~ "^[ ]*"ScriptPid"[ ]*$") )[/tt]

So, presumably, one must quote the awk variables and also delimit their regexp's with quotes as well?

No, if the name of the variable is put in quotes, it becomes a literal string.
Code:
if (($1 ~ "^" UserName "$") && ($2 !~ "^" ScriptPid "$"))
Awk has no operator for string concatenation; the strings are simply juxtaposed.
Code:
BEGIN { food="an apple"; print "I want " food " now." }
produces
[tt]I want an apple now.[/tt]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top