LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
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 04-20-2012, 02:41 AM   #1
masatheesh
Member
 
Registered: Aug 2007
Distribution: CentOS 5.0,CentOS 5.5
Posts: 47

Rep: Reputation: 15
Expand grep command usage


Hi,

I am using grep as below to extract after two lines from line which has words "SENT: RCPT".

grep -B 2 -i "SENT: RCPT" MailLog.txt

Here I need to check another thing also. That is, the line which has words "SENT: RCPT" should not have word "XYZ".

So I need every two lines before a line which contains "SENT: RCPT" and doesnt contain "xyz".

Please help me.
 
Old 04-20-2012, 02:52 AM   #2
fukawi1
Member
 
Registered: Apr 2009
Location: Melbourne
Distribution: Fedora & CentOS
Posts: 854

Rep: Reputation: 193Reputation: 193
It can probably be done with regex, but well, I suck at regex, so I would pipe grep to grep.
Code:
grep -B 2 -i "SENT: RCPT" MailLog.txt | grep -v "xyz"
 
Old 04-20-2012, 03:40 AM   #3
masatheesh
Member
 
Registered: Aug 2007
Distribution: CentOS 5.0,CentOS 5.5
Posts: 47

Original Poster
Rep: Reputation: 15
grep -B 2 -i "SENT: RCPT" MailLog.txt . If I run this, output will be as below,

"SMTPC" 6264 898 "2012-04-19 18:44:39.671" "192.168.0.200" "SENT: MAIL FROM:<abcd@xyz.com>"
"SMTPC" 6264 898 "2012-04-19 18:44:40.015" "192.168.0.200" "RECEIVED: 250 OK"
"SMTPC" 6264 898 "2012-04-19 18:44:40.015" "192.168.0.200" "SENT: RCPT TO:<efgh@xyz.com>"


grep -B 2 -i "SENT: RCPT" MailLog.txt | grep -v "xyz" . If I run this, output will be as below

"SMTPC" 6264 898 "2012-04-19 18:44:40.015" "192.168.0.200" "RECEIVED: 250 OK"

But my requirement is, for example, if third line of first output contains "SENT: RCPT TO:<efgh@domain.com>"
I need to get output as this line and above two lines also.
 
Old 04-20-2012, 03:43 AM   #4
fukawi1
Member
 
Registered: Apr 2009
Location: Melbourne
Distribution: Fedora & CentOS
Posts: 854

Rep: Reputation: 193Reputation: 193
Hang five, I misread your last post.

Last edited by fukawi1; 04-20-2012 at 03:47 AM.
 
Old 04-20-2012, 03:59 AM   #5
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
Hi,

Is this what you are looking for:
Code:
sed '/SENT: MAIL FROM/{N;N;/SENT: RCPT TO/{/xyz/d}}' MailLog.txt
You don't show the full layout of the file, maybe this will suffice:
Code:
sed '/SENT: MAIL FROM/{N;N;/xyz/d}' MailLog.txt
Hope this helps.
 
Old 04-20-2012, 07:30 AM   #6
masatheesh
Member
 
Registered: Aug 2007
Distribution: CentOS 5.0,CentOS 5.5
Posts: 47

Original Poster
Rep: Reputation: 15
I give some more information about my requirement. For example, below lines are excertps from mail log

"SMTPC" 6264 898 "2012-04-19 18:44:39.671" "192.168.0.200" "SENT: MAIL FROM:<abcd@xyz.com>"
"SMTPC" 6264 898 "2012-04-19 18:44:40.015" "192.168.0.200" "RECEIVED: 250 OK"
"SMTPC" 6264 898 "2012-04-19 18:44:40.015" "192.168.0.200" "SENT: RCPT TO:<efgh@xyz.com>"

By using this above three lines, I come to know that mail has sent from abcd@xyz.com to efgh@xyz.com. So this mail communication has happened internally as From mail ID and To mail ID are having same domain as xyz.com. I dont want this internal communication log.

I need to capture all mail communication which sent to domain other than xyz.com. For example, if mail has sent from abc@xyz.com to test@testing.com. Here I will come to know that abc@xyz.com has sent to test@testing.com. So I can capture this user. I want this kind mail logs only.
 
Old 04-20-2012, 08:07 AM   #7
schneidz
LQ Guru
 
Registered: May 2005
Location: boston, usa
Distribution: fedora-35
Posts: 5,313

Rep: Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918
this worx:
Code:
[schneidz@hyper ~]$ cat masatheesh.txt
"SMTPC" 6264 898 "2012-04-19 18:44:39.671" "192.168.0.200" "SENT: MAIL FROM:<abcd@xyz.com>"
"SMTPC" 6264 898 "2012-04-19 18:44:40.015" "192.168.0.200" "RECEIVED: 250 OK"
"SMTPC" 6264 898 "2012-04-19 18:44:40.015" "192.168.0.200" "SENT: RCPT TO:<efgh@xyz.com>"
[schneidz@hyper ~]$ grep -B 2 RCPT masatheesh.txt | awk 'ORS=(NR%3)?" ":"\n"' | grep -v "RCPT TO:.*@xyz"
it essentially takes the 3 lines and appends them into 1 so that grep -v works more easily.
you would need to add some logic if you want to put the non @xyz lines back into 3 separate lines.

Last edited by schneidz; 04-20-2012 at 08:08 AM.
 
Old 04-20-2012, 08:12 AM   #8
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
@masatheesh: Have you tried my solution?
 
Old 04-21-2012, 05:47 AM   #9
masatheesh
Member
 
Registered: Aug 2007
Distribution: CentOS 5.0,CentOS 5.5
Posts: 47

Original Poster
Rep: Reputation: 15
@druuna: I tried. It shows almost all lines. Its not extracting.
 
Old 04-21-2012, 06:05 AM   #10
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
Hi,

It looks like I'm still not clear about what it is you want.

Can you post an unfiltered relevant example of the file you start with and an example of the expected output based on that file?

This is what I assumed based on your previous posts:
Code:
$ cat infile
"SMTPC" 6264 898 "2012-04-19 18:44:39.671" "192.168.0.200" "SENT: MAIL FROM:<abcd@xyz.com>"
"SMTPC" 6264 898 "2012-04-19 18:44:40.015" "192.168.0.200" "RECEIVED: 250 OK"
"SMTPC" 6264 898 "2012-04-19 18:44:40.015" "192.168.0.200" "SENT: RCPT TO:<efgh@xyz.com>"
"SMTPC" 6264 898 "2012-04-19 18:44:39.671" "192.168.0.200" "SENT: MAIL FROM:<abcd@domain.com>"
"SMTPC" 6264 898 "2012-04-19 18:44:40.015" "192.168.0.200" "RECEIVED: 250 OK"
"SMTPC" 6264 898 "2012-04-19 18:44:40.015" "192.168.0.200" "SENT: RCPT TO:<efgh@domain.com>"

$ sed '/SENT: MAIL FROM/{N;N;/SENT: RCPT TO/{/xyz/d}}' infile 
"SMTPC" 6264 898 "2012-04-19 18:44:39.671" "192.168.0.200" "SENT: MAIL FROM:<abcd@domain.com>"
"SMTPC" 6264 898 "2012-04-19 18:44:40.015" "192.168.0.200" "RECEIVED: 250 OK"
"SMTPC" 6264 898 "2012-04-19 18:44:40.015" "192.168.0.200" "SENT: RCPT TO:<efgh@domain.com>"
The italic lines contain RCPT TO. And you only want those lines and 2 before it when it does not contain xyz. My solution seems to do just that.

Please elaborate.
 
Old 04-25-2012, 12:41 AM   #11
masatheesh
Member
 
Registered: Aug 2007
Distribution: CentOS 5.0,CentOS 5.5
Posts: 47

Original Poster
Rep: Reputation: 15
For example consider below log file content,

"SMTPC" 6264 898 "2012-04-19 18:44:39.671" "192.168.0.200" "SENT: MAIL FROM:<abcd@xyz.com>"
"SMTPC" 6264 898 "2012-04-19 18:44:40.015" "192.168.0.200" "RECEIVED: 250 OK"
"SMTPC" 6264 898 "2012-04-19 18:44:40.015" "192.168.0.200" "SENT: RCPT TO:<efgh@xyz.com>"
"POP3D" 12160138 "2012-04-19 13:42:41.984" "192.168.0.100" "RECEIVED: AUTH "
"POP3D" 12160138 "2012-04-19 13:42:41.984" "192.168.0.100" "SENT: -ERR Invalid command in current state."
"POP3D" 6264 138 "2012-04-19 13:42:42.000" "192.168.0.100" "RECEIVED: USER It@xyz.com"
"POP3D" 6264 138 "2012-04-19 13:42:42.000" "192.168.0.100" "SENT: +OK Send your password"
"POP3D" 11736 138 "2012-04-19 13:42:42.000" "192.168.0.100" "RECEIVED: PASS ***"
"POP3D" 11736 138 "2012-04-19 13:42:42.000" "192.168.0.100" "SENT: +OK Mailbox locked and ready"
"POP3D" 11736 138 "2012-04-19 13:42:42.015" "192.168.0.100" "RECEIVED: STAT"
"POP3D" 11736 138 "2012-04-19 13:42:42.015" "192.168.0.100" "SENT: +OK 0 0"
"POP3D" 11736 138 "2012-04-19 13:42:42.031" "192.168.0.100" "RECEIVED: QUIT"
"POP3D" 11736 138 "2012-04-19 13:42:42.031" "192.168.0.100" "SENT: +OK POP3 server saying goodbye..."
"SMTPC" 6264 898 "2012-04-19 18:44:39.671" "192.168.0.200" "SENT: MAIL FROM:<abcd@xyz.com>"
"SMTPC" 6264 898 "2012-04-19 18:44:40.015" "192.168.0.200" "RECEIVED: 250 OK"
"SMTPC" 6264 898 "2012-04-19 18:44:40.015" "192.168.0.200" "SENT: RCPT TO:<efgh@domain.com>"

"POP3D" 6264 138 "2012-04-19 13:42:42.000" "192.168.0.100" "SENT: +OK Send your password"
"POP3D" 11736 138 "2012-04-19 13:42:42.000" "192.168.0.100" "RECEIVED: PASS ***"
"POP3D" 11736 138 "2012-04-19 13:42:42.000" "192.168.0.100" "SENT: +OK Mailbox locked and ready"
"POP3D" 11736 138 "2012-04-19 13:42:42.015" "192.168.0.100" "RECEIVED: STAT"
"POP3D" 11736 138 "2012-04-19 13:42:42.015" "192.168.0.100" "SENT: +OK 0 0"
"POP3D" 11736 138 "2012-04-19 13:42:42.031" "192.168.0.100" "RECEIVED: QUIT"
"POP3D" 11736 138 "2012-04-19 13:42:42.031" "192.168.0.100" "SENT: +OK POP3 server saying goodbye..."
"SMTPC" 6264 898 "2012-04-19 18:44:39.671" "192.168.0.200" "SENT: MAIL FROM:<gh@xyz.com>"
"SMTPC" 6264 898 "2012-04-19 18:44:40.015" "192.168.0.200" "RECEIVED: 250 OK"
"SMTPC" 6264 898 "2012-04-19 18:44:40.015" "192.168.0.200" "SENT: RCPT TO:<xyz@domain.com>"



From above log, I need output as Italic marked lines
 
Old 04-25-2012, 12:48 AM   #12
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
Hi,

Code:
sed -n '/SENT: MAIL FROM/{N;N;/SENT: RCPT TO/{/domain/p}}' infile
A test run on the above given data:
Code:
$ sed -n '/SENT: MAIL FROM/{N;N;/SENT: RCPT TO/{/domain/p}}' infile
"SMTPC" 6264 898 "2012-04-19 18:44:39.671" "192.168.0.200" "SENT: MAIL FROM:<abcd@xyz.com>"
"SMTPC" 6264 898 "2012-04-19 18:44:40.015" "192.168.0.200" "RECEIVED: 250 OK"
"SMTPC" 6264 898 "2012-04-19 18:44:40.015" "192.168.0.200" "SENT: RCPT TO:<efgh@domain.com>"
"SMTPC" 6264 898 "2012-04-19 18:44:39.671" "192.168.0.200" "SENT: MAIL FROM:<gh@xyz.com>"
"SMTPC" 6264 898 "2012-04-19 18:44:40.015" "192.168.0.200" "RECEIVED: 250 OK"
"SMTPC" 6264 898 "2012-04-19 18:44:40.015" "192.168.0.200" "SENT: RCPT TO:<xyz@domain.com>
Hope this helps.
 
Old 05-19-2012, 04:43 AM   #13
masatheesh
Member
 
Registered: Aug 2007
Distribution: CentOS 5.0,CentOS 5.5
Posts: 47

Original Poster
Rep: Reputation: 15
Hi,

Thanks for your time and effort.

If a line which has "RCPT TO" and doesnt have xyz.com, then the command has to give that line and two lines before also. Because sometimes domain.com can be replaced by domain1.com,domain2.com etc.

Once again thanks for your help.
 
Old 05-19-2012, 05:32 AM   #14
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
Guess the info given in post #11 isn't relevant after all.....

Try this:
Code:
sed -n '/SENT: MAIL FROM/{N;N;{/SENT: RCPT TO:.*xyz.com>/!p}}' infile
 
Old 05-19-2012, 05:38 AM   #15
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,848

Rep: Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309
If you do not need the complete lines you can do the following:
Code:
awk ' BEGIN { RS="SENT: MAIL FROM"; FS="\n" } index($3, "xyz.com") == 0 { print $1 "\n" $2 "\n" $3 } ' inputfile
will display the relevant data I think.
 
  


Reply



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
correct usage of grep to search a disk Completely Clueless Linux - Newbie 13 02-10-2011 01:41 AM
How to expand who command? SentralOrigin Linux - Newbie 7 08-27-2010 12:38 PM
basic vi / grep usage opensource82 Linux - Newbie 10 07-07-2008 04:22 AM
grep usage with regular expression jonathanztaub Linux - General 7 09-01-2004 10:35 PM
Grep Usage specialist01 Linux - General 2 06-13-2003 07:52 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 04:54 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
Open Source Consulting | Domain Registration