Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I seem to match two things using extended regular expressions:

  1. Newline. I have tried [ \n], [ \\n], both don't work
  2. Negative lookahead for string "timeout". I have tried (?!timeout)

Can anyone please point out the correct way?

share|improve this question
    
If it is against a DOS encoded text file, or may be used against one at some point, try matching [\n\r] to allow for carriage return as well. – Justin Feb 11 '13 at 2:51
up vote 3 down vote accepted

Newline

If you're using bash, you can write $'\n'. This gets expanded by the shell, not the program, so it's more reliable than other options.

If you're using dash (e.g., inside a shell script), you can always use a literal quoted newline:

'
'

Not elegant, but it works.

Lookahead

You'll have to use Perl Compatible Regular Expressions (-P), since extended regular expressions do not support lookaheads.

Oftentimes, it's sufficient to mimic negative lookaheads.

Using grep's -v switch usually accomplishes this:

grep -E 'PATTERN' | grep -vE 'PATTERNtimeout'

is equivalent to

grep -P 'PATTERN(?!timeout)'

To mimic (?!timeout) inside another expression, you can use this subexpression:

(($|[^t])|t($|[^i])|ti($|[^m])|tim($|[^e])|time($|[^o])|timeo($|[^u])|timeou($|[^t]))
  • ($|[^t]) string ends here or first character is not t.
  • t($|[^i]) string ends after t or second character is not i.
  • ti($|[^m]) string ends after ti or third character is not tim.
  • ...

If any of the above matches, the string is not timeout.

share|improve this answer

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .