LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (http://www.linuxquestions.org/questions/linux-general-1/)
-   -   Paragraph-option to grep (http://www.linuxquestions.org/questions/linux-general-1/paragraph-option-to-grep-425331/)

TheSpork 03-16-2006 06:46 AM

Paragraph-option to grep
 
I've got a huge logfile (~5mill lines) in text format which is actually a maillog. The log is formatted like this:

----- New message date time -----
From: someone@example.com
Sender: someone@example.com
Replay: someone@example.com
To: someoneelse@example.org
CC:
BCC:
Subject: Example
Mail id: 12345
Mail name: /tmp/mail_12345.eml
---------------------------------------

Every entry is inserted like the example above.

In AIX, the grep-command has a option "-p" which allows you to select a delimiter for a paragraph (here: -----), so when grep finds something, everything between the delimiters are printed out. I'm searching through tons of mail, but only want to get the output with selected words.

Is there a similar possibility in Linux? Either a different option to grep (which I haven't found), or a standalone script / program..

Does anyone know?

druuna 03-16-2006 10:22 AM

Hi,

Using awk:

awk '{ FS="\n" ; RS="-----" } /12345/ { print }' infile

Or as a shellscript:
Code:

#!/bin/bash

# usage: pargrep <infile> <searchpattern>

inFile="$1"
searchString="$2"

awk '
BEGIN {
        FS="\n"
        RS="-----"
}
/'"$searchString"'/ { print }
' ${inFile}

You should include some checks (is input present, things like that).

Hope this helps.

TheSpork 03-17-2006 02:45 AM

Thanks a lot! :) This was exactly what I was looking for!


All times are GMT -5. The time now is 08:49 AM.