I am using tcl to perform some pattern matching. The following is the string that I am performing the match on:
ps -ef | grep ipqosmgr
root 17255 17136 0 22:34 ttyS0 00:00:00 grep ipqosmgr
root 28986 17731 0 Jun05 ? 00:02:01 /isan/bin/ipqosmgr
Typically I would want the 3rd line
root 28986 17731 0 Jun05 ? 00:02:01 /isan/bin/ipqosmgr
since I would like the process ID associated with the process.
When I use the following regular expression, it works as expected:
% foreach line [split $output \n] {
if { [ regexp -nocase {root\s+([0-9]+)\s+.*(/isan/bin/ipqosmgr)} $line - value ] } {
puts $line
}
}
root 28986 17731 0 Jun05 ? 00:02:01 /isan/bin/ipqosmgr
% puts $value
28986
%
However, I would like this code to run for multiple processes and hence have put this in a function with $process which will hold the value of the process. When I use the same regex with the variable $process, it fails.
% puts $process
ipqosmgr
%
% foreach line [split $output \n] {
if { [ regexp -nocase {root\s+([0-9]+)\s+.*(/isan/bin/$process)} $line - value ] } {
puts $line
}
}
%
% puts $value
can't read "value": no such variable
%
I have no idea as to why its behaving this way and it would be really great if someone could tell me whats going wrong here and how to rectify it.
"root\\s+(\[0-9\]+)\\s+.*(/isan/bin/$process)"if you plan to use string interpolation.