I'm trying to use a regex in egrep on Centos 5.10, based on the one I made from this question on SO.

T12345 .{1,100}[\n\r].{1,100}2014.03.04

I can successfully match the regex to the content in my browser, but when I try it in an egrep, nothing is returned, even though there are several matches in my directory.

Working Regex Example here

Here is the egrep as I'm using it.

egrep -l "T12345 .{1,100}[\n\r].{1,100}2014.03.04" logfiles_*.txt

As far as I can tell, I'm not escaping the restricted characters properly, but nothing I've tried seems to work. How can I adapt this regex for use in egrep?

  • 1
    grep operates on lines, and your expression will never match a single line.
    – jordanm
    Mar 10 '14 at 12:34
  • 1
    As an aside, please post more information in your questions. In your previous question on SO, you should have mentioned that you are using grep. If a solution did not work for you, what prompted you to accept the answer and post a question on a different site?
    – devnull
    Mar 10 '14 at 12:37
  • Replace egrep -l with pcregrep -Ml
    – janos
    Mar 10 '14 at 12:39
  • @devnull Because I tried to avoid asking a multipart question equivalent to "just do this for me". I like to abstract questions so they are more widely useful too. Regexes are not just used by egrep in Linux, so if another person finds that question they can use the answer. Also now I know the regex works, I tried to implement it myself. I couldn't, so now the implementation in Centos is the problem, it becomes a question suitable for U&L.
    – blarg
    Mar 10 '14 at 12:47
  • @blarg The reason for the comment was simply to state that the implementation of regular expressions varies from language to language. As such, the solutions for different languages might be rather different!
    – devnull
    Mar 10 '14 at 12:50

grep matches lines of input. If you want to match over multiple lines, use another utility. For example, use pcregrep:

pcregrep -M "T12345 .{1,100}[\n\r].{1,100}2014.03.04" filename

This would produce:

31/01/2014 11:15:40 |  Bla Bla Bla Bla T12345 Bla Bla Bla
31/01/2014 11:15:40 | Bla Bla Bla 2014/03/04 Bla Bla Bla

for your input.

  • (bearing in mind that . above would not match a newline character) Mar 10 '14 at 12:41
  • @StephaneChazelas Yes, I assumed that the regex is intended for matching over two lines! (From the linked question on SO.)
    – devnull
    Mar 10 '14 at 12:43

Your problem is that egrep and grep normally reads a single line and then compare to the given regexp.

To make your regexp solutions works, you need the -z option for egrep.

-z, --null-data
Treat the input as a set of lines, each terminated by a zero byte (the ASCII NUL character) instead of a newline. Like the -Z or --null option, this option can be used with commands like sort -z to process arbitrary file names.

So try this: egrep -l -z "T12345 .{1,100}\s+.{1,100}2014.03.04" logfiles_*.txt

  • \s+ will match any whitespace, not \r and \n specifically.
    – terdon
    Mar 10 '14 at 14:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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