LinuxQuestions.org
Latest LQ Deal: Linux Power User Bundle
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 07-01-2013, 09:11 AM   #1
andre3050
LQ Newbie
 
Registered: Jun 2013
Posts: 14

Rep: Reputation: Disabled
Pick out contents of a file


Hi

I have used grep to produce a file from the mail log, but I would like to know if it is possible to just show the contents within < >, ie, the email address only and just print that to a file.

Jun 27 00:50:08 245ML postfix/error[13259]: EF1A2189E88: to=<person@email.com>, relay=none, delay=18141,
delays=0.5/18140/0/0.01, dsn=4.4.3, status=deferred (delivery temporarily suspended: Host or domain name not found. Name
service error for name=email.com type=MX: Host not found, try again)

Thanks
 
Old 07-01-2013, 09:22 AM   #2
schneidz
LQ Guru
 
Registered: May 2005
Location: boston, usa
Distribution: fc-15/ fc-20-live-usb/ aix
Posts: 5,150

Rep: Reputation: 887Reputation: 887Reputation: 887Reputation: 887Reputation: 887Reputation: 887Reputation: 887
Code:
awk -F'<|>' '{print $2}' andre3050.txt
 
3 members found this post helpful.
Old 07-01-2013, 10:20 AM   #3
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,552

Rep: Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898
You could stay with grep if you like:
Code:
grep -Po '(?<=<)[^>]*' file
 
3 members found this post helpful.
Old 07-01-2013, 10:32 AM   #4
andre3050
LQ Newbie
 
Registered: Jun 2013
Posts: 14

Original Poster
Rep: Reputation: Disabled
Wow that works great!
Thanks schneidz you have saved me many of hours!
 
Old 07-02-2013, 02:39 PM   #5
andre3050
LQ Newbie
 
Registered: Jun 2013
Posts: 14

Original Poster
Rep: Reputation: Disabled
Thanks guys both work great.

Been trying but not been able to also add another two search strings.
Most of the errors we get are said: 550 or said: said: 550-5.1.1, so how can I add these two? So it would:
- Print the email address
- Print either of the two errors for the relevant email

Thanks

Last edited by andre3050; 07-02-2013 at 02:48 PM.
 
Old 07-02-2013, 02:51 PM   #6
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,552

Rep: Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898
Now you are getting a little trickier ... please provide some relevant examples as where this is in the data will alter the command. grep may also not be an option now
 
Old 07-02-2013, 03:28 PM   #7
andre3050
LQ Newbie
 
Registered: Jun 2013
Posts: 14

Original Poster
Rep: Reputation: Disabled
Heres an example of the errors:


Jun 28 13:51:19 245ML postfix/smtp[1637]: 230D77011DE: to=<sss@gmail.com>, relay=gmail-smtp-in.l.google.com[143.194.78.27]:25, delay=27310, delays=24856/2447/3.4/3.1, dsn=5.1.1, status=bounced (host gmail-smtp-in.l.google.com[153.191.70.27] said: 550-5.1.1 The email account that you tried to reach does not exist. Please try 550-5.1.1 double-checking the recipient's email address for typos or 550-5.1.1 unnecessary spaces. Learn more at 550 5.1.1 http://support.google.com/mail/bin/a...py?answer=6596 r42si4452088eep.122 - gsmtp (in reply to RCPT TO command))

Jun 28 13:52:31 245ML postfix/smtp[30134]: E0A3C16A7F2: to=<blahblah@hotmail.com>, relay=mx2.hotmail.com[65.55.37.104]:25, delay=1361, delays=1179/151/31/0.43, dsn=5.0.0, status=bounced (host mx2.hotmail.com[65.55.37.104] said: 550 Requested action not taken: mailbox unavailable (in reply to RCPT TO command))

Jun 28 13:57:09 245ML postfix/smtp[1126]: 62A4517AA13: to=<jklhah@mail.com>, relay=mail.xxxxx.xx[111.111.111.111]:25, delay=3.2, delays=0.01/0/3.2/0.04, dsn=5.1.1, status=bounced (host mail.xxxxx.xx[111.111.111.111] said: 550 5.1.1 <jklhah@mail.com> is not a valid mailbox (in reply to RCPT TO command))

Thanks for your help
 
Old 07-02-2013, 08:34 PM   #8
schneidz
LQ Guru
 
Registered: May 2005
Location: boston, usa
Distribution: fc-15/ fc-20-live-usb/ aix
Posts: 5,150

Rep: Reputation: 887Reputation: 887Reputation: 887Reputation: 887Reputation: 887Reputation: 887Reputation: 887
[quick-and-dirty]:
Code:
 egrep -o "(<.*>,|said: 550 5.1.1|said: 550)" andre3050.txt
 
Old 07-03-2013, 05:02 AM   #9
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,552

Rep: Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898
hmmm ... should have added, what format do you want the output in?
 
Old 07-03-2013, 05:24 AM   #10
andre3050
LQ Newbie
 
Registered: Jun 2013
Posts: 14

Original Poster
Rep: Reputation: Disabled
Hey grail

Thanks for that. I can't wait to try it when i'm able to later.
Well it would be good to have it in a format that can be used in a MySQL database, if that's what you mean?
 
Old 07-03-2013, 07:54 AM   #11
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,552

Rep: Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898
Actually, I was asking for you to show us what format you would like?

For instance, based on your example data and using schneidz's command the output would look like:
Code:
<sss@gmail.com>,
said: 550
<blahblah@hotmail.com>,
said: 550
<jklhah@mail.com>,
said: 550 5.1.1
Is this acceptable?
 
1 members found this post helpful.
Old 07-03-2013, 08:45 AM   #12
andre3050
LQ Newbie
 
Registered: Jun 2013
Posts: 14

Original Poster
Rep: Reputation: Disabled
Haha, I did wonder that!
Have each email and error on one line after the comma maybe?

Thanks
 
Old 07-03-2013, 09:07 AM   #13
schneidz
LQ Guru
 
Registered: May 2005
Location: boston, usa
Distribution: fc-15/ fc-20-live-usb/ aix
Posts: 5,150

Rep: Reputation: 887Reputation: 887Reputation: 887Reputation: 887Reputation: 887Reputation: 887Reputation: 887
there are probably way more elegant solutions:
Code:
[schneidz@hyper ~]$ egrep -o "(<.*>,|said: 550 5.1.1|said: 550)" andre3050.txt | tr '\n' ' ' | tr '\<' '\n'

sss@gmail.com>, said: 550 
blahblah@hotmail.com>, said: 550 
jklhah@mail.com>, said: 550 5.1.1 [schneidz@hyper ~]$
 
1 members found this post helpful.
Old 07-03-2013, 09:25 AM   #14
andre3050
LQ Newbie
 
Registered: Jun 2013
Posts: 14

Original Poster
Rep: Reputation: Disabled
so cool, thank you so much guys.
 
Old 07-03-2013, 10:41 AM   #15
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,552

Rep: Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898
I would probably go with sed:
Code:
sed -rn 's/.*<([^>]*).*(said: 550([- ]5\.1\.1)?).*/\1,\2/p' file
 
  


Reply


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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
How to pick up a file that has been created latest ? Sudharshana Linux - Newbie 3 02-20-2013 04:51 AM
how to pick random file name from a list of filenames in a text file. pdklinux79 Linux - Newbie 9 06-20-2008 03:46 PM
howto pick bash variables from file imagineers7 Linux - Newbie 3 05-08-2006 02:56 AM
I need to pick out the first, last and how many from a file...?!!? vous Programming 3 03-22-2005 02:44 PM
How to pick a string from a text file? sdandeker Linux - General 1 02-12-2004 05:17 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 05:13 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration