Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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
add comment

1 Answer

up vote 2 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
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.