1

I am trying to come up with an egrep command that can do pattern match a set of strings and variables. So far, I am using the following:

ps -ef <uid> | egrep "string1|string2" | egrep -v "string4|string5" | grep -v "${variable}"

This works, but I am trying to have a syntax that is uniform and efficient.

I could get the following command work on Linux but not on AIX:

ps -ef <uid> | egrep "string1|string2" | grep -v "${variable}\|string4\|string5" 

I have gone through the man pages and tried egrep -c and such but could not make any combination work.

Jay
  • 11
  • 1
  • 3

1 Answers1

2

You're mixing grep and egrep commands together, and have dropped the required -E flag for grep so that it parses | as the alternation regular expression. Alternatively use egrep all the way through.

Use one of these:

ps -ef | egrep "string1|string2" | egrep -v "${variable}|string4|string5"

or

ps -ef | egrep "string1|string2" | grep -Ev "${variable}|string4|string5"

... being careful in either one to escape regular expression tokens in variable (or the various stringN's, for that matter).

See also: Why does my regular expression work in X but not in Y?

Jeff Schaller
  • 67,283
  • 35
  • 116
  • 255