search and extract


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting search and extract
# 1  
Old 04-04-2010
search and extract

Not sure if a similar question has been asked before. I have the following e-mail log:

Code:
Apr  1 00:00:30 my.mailserver.com sm-mta[16406]: [ID 801593 mail.info] o3150TeJ016406: from=<address@domain.com>, size=3152, class=0, nrcpts=1, msgid=<507309.10858461270098026797>, proto=ESMTP, daemon=MTA, tls_verify=NONE, auth=NONE, relay=mail-rserver.com [xx.xx.xx.xxx]
Apr  1 00:00:30 my.mailserver.com MM: [ID 702911 mail.info] [Jilter Processor 24 - Async Jilter Worker 18 - 127.0.0.1:60292-o3150TeJ016406] INFO  user.log  - mtaqid=o3150TeJ016406, msgid=<507309.10858461270098026797>, from=<address@domain.com>, size=3203, to=<address1@domain1.com>, relay=[xx.xx.xx.xxx], disposition=Deliver
Apr  1 00:00:30 my.mailserver.com sm-mta[16430]: [ID 801593 mail.info] o3150TeJ016406: to=<address1@domain1.com>, delay=00:00:00, xdelay=00:00:00, mailer=relay, pri=123152, relay=[yy.yy.yy.yy], dsn=2.0.0, stat=Sent ( <507309.10858461270098026797> Queued mail for delivery)


Here is what I need to do:

- look for the first occurrence of the string "from=" and capture the string in between "<" and ">"
- look for the first occurrence of the string "relay" and capture the string in between "=" and "]"
- look for the occurrence of the string "to=" and capture the string in between "<" and "," but up until the first space appears
- look for the second occurrence of the string "relay=" and capture the string in between "[" and "]"
- look for the first occurrence of the string "sent" and capture the string between "=" and " " (space)


If I am looking this wrong, please let me know. Thank you in advance

Last edited by Franklin52; 04-04-2010 at 11:40 AM.. Reason: Please use code tags!
# 2  
Old 04-05-2010
MySQL

I cant understand you clearly but I want to help you.
For example 1 and 2 matter that you write
I try to write a code maybe you want like this..

for 1.matter
Code:
 
sed 's/\(.*from=<\)\([a-z][a-z]*@[a-z][a-z]*\)\(>*.*\)/\2/g' log
address@domain
address@domain
Apr  1 00:00:30 my.mailserver.com sm-mta[16430]: [ID 801593 mail.info] o3150TeJ016406: to=<address1@domain1.com>, delay=00:00:00, xdelay=00:00:00, mailer=relay, pri=123152, relay=[yy.yy.yy.yy], dsn=2.0.0, stat=Sent ( <507309.10858461270098026797> Queued mail for delivery)

for 2.matter
Code:
sed 's/\(.*relay=\)\(.*-*.*\)\(].*\)/\2/g' log
mail-rserver.com [xx.xx.xx.xxx
[xx.xx.xx.xxx
[yy.yy.yy.yy

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Extract fields before search pattern

Hi, I have below file structure and need to display hours, minutes and seconds as different fields. Incase hour or minute field is not there it should default to zero. *** Total elapsed time was 2 hours, 54 minutes and 40 seconds. *** Total elapsed time was 42 minutes and 36 seconds.... (7 Replies)
Discussion started by: fristyguy
7 Replies

2. Shell Programming and Scripting

Need to extract characters between two search words in a script!!

Hi, I have a log file which is the output from a xml script : <?xml version="1.0" ?> <!DOCTYPE svc_result SYSTEM "MLP_SVC_RESULT_320.DTD"> <svc_result ver="3.2.0"> <slia ver="3.0.0"> <pos> <msid type="MSISDN" enc="ASC">8093078040</msid> <poserr> ... (4 Replies)
Discussion started by: arjunstarz
4 Replies

3. Shell Programming and Scripting

Search for a pattern,extract value(s) from next line, extract lines having those extracted value(s)

I have hundreds of files to process. In each file I need to look for a pattern then extract value(s) from next line and then search for value(s) selected from point (2) in the same file at a specific position. HEADER ELECTRON TRANSPORT 18-MAR-98 1A7V TITLE CYTOCHROME... (7 Replies)
Discussion started by: AshwaniSharma09
7 Replies

4. UNIX for Dummies Questions & Answers

Search and extract matching patterns

%%%%% (9 Replies)
Discussion started by: lucasvs
9 Replies

5. Shell Programming and Scripting

help with Scripting - trying to search for string and extract next few characters

Hi I am new to world on unix scripting so any assistance would be gratefully appreciated, I am trying to write a script which reads through a file, reads in line by line, searches for a pattern, copies string after it and then to do a search and replace elsehwere in the line, so the... (7 Replies)
Discussion started by: LonJ_80
7 Replies

6. Shell Programming and Scripting

Search and Extract data between two strings

hi, In a given directory, i need to search for a string (eg:ABCD). For a given file, i have to extract the text between START and END strings . I need to extract all the text between START and END and there can be multiple START and END in a file. Sample: There is a directort... (3 Replies)
Discussion started by: flamingo_l
3 Replies

7. Shell Programming and Scripting

Extract data based on specific search criteria

I have a huge file (about 2 millions records) contains data separated by “,” (comma). As part of the requirement, I can't change the format. The objective is to remove some of the records with the following condition. If the 23rd field on each line start with 302 , I need to remove that from the... (4 Replies)
Discussion started by: jaygamini
4 Replies

8. Shell Programming and Scripting

Extract two lines before and after the 'search text'

Hi Guys, I have a situation wherein I need to extract two lines from below the search string. Eg. Current: $ grep "$(date +'%a %b %e')" alert.log Mon Apr 12 03:58:10 2010 Mon Apr 12 12:51:48 2010 $ Here I would like the display to be something like Mon Apr 12... (6 Replies)
Discussion started by: geetap
6 Replies

9. Shell Programming and Scripting

Search and extract by section from configuration

Hi, I understand either AWK or SED can do this, but I not sure how to extract the following configuration in section. Meaning when I need to find code with " ip helper-address 192.168.11.2" , it would start from "interface Serial0/0" and "interface FastEthernet0/1". Only displaying both section... (2 Replies)
Discussion started by: haphazard
2 Replies

10. Shell Programming and Scripting

Perl script to search and extract using wildcards.

Good evening All, I have a perl script to pull out all occurrences of a files beginning with xx and ending in .p. I will then loop through all 1K files in a directory. I can grep for xx*.p files but it gives me the entire line. I wish to output to a single colum with only the hits found. ... (3 Replies)
Discussion started by: CammyD
3 Replies
Login or Register to Ask a Question