2 ms·
The grep above will match itself in the output. One way that I have seen to filter it out is grep -v grep, but if you are actually looking for grep processes, t
by chasil 5y ago
The grep above will match itself in the output. One way that I have seen to filter it out is grep -v grep, but if you are actually looking for grep processes, then this won't work.
The shell can also filter these out. Consider this basic shell function:
pps () (
IFS=\|
ps -ef |
while read a
do case $a in
*$1*|+([!0-9])) printf %s\\n "$a";;
esac
done
)
This uses a shell pattern instead of a regular expression (shells with more features than POSIX can also use regex). It's actually a dual pattern, either no numbers which match the header, or a match of the pattern specified as the first function argument.
The function uses a subshell () instead of command grouping {}. This allows IFS to be manipulated without modifying this setting outside the function, at the cost of a fork. POSIX doesn't support local shell variables; if your shell does (with typeset or local keywords), it will be slightly more efficient.
My function would slot into yours, but yours would not work in the Debian default (dash) shell, because the "function" keyword is not defined in POSIX.
POSIX approaches are the most important, because you can use them just about everywhere.