LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices

Reply
 
Search this Thread
Old 03-16-2006, 06:46 AM   #1
TheSpork
Member
 
Registered: May 2003
Location: Norway
Distribution: Ubuntu / Debian
Posts: 40

Rep: Reputation: 15
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?
 
Old 03-16-2006, 10:22 AM   #2
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374
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.
 
Old 03-17-2006, 02:45 AM   #3
TheSpork
Member
 
Registered: May 2003
Location: Norway
Distribution: Ubuntu / Debian
Posts: 40

Original Poster
Rep: Reputation: 15
Thanks a lot! This was exactly what I was looking for!
 
  


Reply

Tags
awk


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to use "OR" option in a grep command. sumitarun Programming 3 04-18-2005 02:44 AM
grep option needed ziox Linux - General 7 01-05-2005 12:02 AM
Latex question, image and paragraph ordering.. doublefailure Linux - Software 3 10-30-2004 09:40 PM
Converting a paragraph into a one-line string ganninu Linux - General 4 12-03-2003 08:50 AM
Word count in paragraph - Open Office, Sutekh Linux - Software 10 04-19-2003 11:27 PM


All times are GMT -5. The time now is 07:12 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration