4

I want to find a particular process using awk:

ps aux|awk '/plugin-container.*Flash.*/'

Now it finds the process, but it includes itself in the results, because ps results include them as well. To prevent that, I am trying to use negative look behind as follows:

ps aux|awk '/(\?<!awk).*plugin-container.*Flash.*/'

But it does not work. Does awk support look behind? What am I doing wrong? Thanks

Loke
  • 249
  • 1
  • 6
  • 14

3 Answers3

7

The common trick is to use

ps aux | grep '[p]lugin-container.*Flash.*'

The character class [p] prevents grep itself from being matched.

choroba
  • 231,213
  • 25
  • 204
  • 289
  • 1
    The same trick works in awk: `ps aux | awk '/[p]lugin-container.*Flash.*/'` – Dennis Williamson Jan 26 '12 at 17:27
  • This works well, even with awk. Can you please explain how it works? Thanks – Loke Jan 27 '12 at 16:42
  • @Loke: I tried to explain it in the last line. `[p]` creates a character class that only the character `p` matches, thus `plugin` matches, but `[p]lugin` does not. In other words, you change the command line not to contain the string it searches for. – choroba Jan 27 '12 at 16:46
2

I don't know whether awk supports lookbehind, but I usually solve this problem with grep -v:

aix@aix:~$ ps aux | awk '/plugin-container.*Flash/' | grep -v awk

(Also, I'd normally use grep for the awk command above.)

NPE
  • 486,780
  • 108
  • 951
  • 1,012
0

I don't know if awk supports lookbehinds, but if, then the question mark at the start should not be escaped, a negative lookbehind starts (?<!

A question mark right after the opening bracket is the sign, that this group is not a capturing group, i.e. it has some special meaning.

stema
  • 90,351
  • 20
  • 107
  • 135