The man page for the pgrep
command indicates that the pattern "specifies an Extended Regular Expression for matching against the process names or command lines". Apparently, Extended Regular Expressions are an extension of the regular expressions used in the original UNIX grep command.
I need to be able to match the string 'myProcess'
but exclude the string 'test_myProcess'
using Extended Regular Expressions. Initially, I assumed that Extended Regular Expressions would support the negative lookbehind assertion, which led me to the following regular expression: (?<!test_)(myProcess)
. This apparently works for other regular expression tools (I tried it on the Regex101 website).
However, when I try it with pgrep
, I get the following error: Invalid preceding regular expression
. After looking at some Extended Regular Expression pages, it appears that the negative lookbehind assertion doesn't exist for this type of regular expression (or at least I was unable to find any reference to it). I need to know how to match the string
'myProcess'
but exclude the string `'test_myProcess' using Extended Regular Expressions.
Edit: Examples of text I want to match (or not match) Match desired:
- myProcess
- /usr/local/bin/myProcess.py
- /usr/bin/myProcess
Match NOT desired:
- test_myProcess
- /usr/local/bin/test_myProcess.py
- /usr/bin/test_myProcess
Right now, I am toying with the idea of running pgrep twice, once matching 'myProcess'
and once matching 'test_myProcess'
and taking the difference of the two results as the correct answer. However, I am definitely interested in alternative solutions.
Edit:
Note that the process name itself will not include 'myProcess'
. It will simply appear in the command-line for that process. The process name will most likely be python2.7
. And, 'myProcess'
is not guaranteed to occupy either the beginning or the end of the string. Also, as pointed out, I want to match the string if 'myProcess'
is present but not if 'test_myProcess'
is also present.
grep
supports Perl Compatible Regular Expressions (PCRE). Use the-P
option; e.g.grep -P "(?<test_)myProcess"