LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 07-05-2007, 11:31 PM   #1
retrovertigo
Member
 
Registered: Jul 2007
Distribution: Arch Linux
Posts: 36

Rep: Reputation: 15
Bash scripting question re: newlines


I'm writing a bash script to search the contents of a postfix log. The results are stored in a variable called "results". To keep the script's output readable (since this variable contains multiple lines from the log) I am setting the IFS variable to an empty string so that the line breaks in my grep results are preserved.

The result set that I wish to search will look like this:

Code:
Jul  3 23:12:14 mf04 postfix-mxmf/qmgr[6250]: CCB2825AE1C: from=<test@test.com>, size=2546, nrcpt=2 (queue active) 
Jul  3 23:12:14 mf04 postfix-mxmf/smtp[31012]: CCB2825AE1C: to=<user1@domain.com>, relay=some.server.address[192.168.30.201]:25, delay=8.2, delays=8.1/0.01/0/0.02, dsn=2.0.0, status=sent (250 low[1484]) 
Jul  3 23:12:14 mf04 postfix-mxmf/smtp[31012]: CCB2825AE1C: to=<user2@domain.com>, orig_to=<user1@domain.com>, relay=some.server.address[192.168.30.201]:25, delay=8.2, delays=8.1/0.01/0/0.02, dsn=2.0.0, status=sent (250 low[1484]) 
Jul  3 23:12:14 mf04 postfix-mxmf/qmgr[6250]: CCB2825AE1C: removed
I want to take these results and filter out the lines with "postfix-mxmf/smtp" and just process those. For each line, I want to grab the email address and the "low[somenumber]". Both of these I can do with regexes, that's no problem. It's processing the output line by line that I'm having trouble with.

The end result I want is that, for each "postfix-mxmf/smtp" line, I grep a different logfile for the email address and "low[somenumber]" and return a line that shows that the message was delivered, like so:

Code:
Jul  3 23:11:39 drd00 bingo[22660]: rfc2821: low[1484]: to=<user1@domain.com>, delay=0, status=sent 
Jul  3 23:11:39 drd00 bingo[22660]: rfc2821: low[1484]: to=<user2@domain.com>, delay=0, status=sent

Any ideas?
 
Old 07-06-2007, 02:28 AM   #2
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682
I don't know why you would want to read a log into a variable. You could use sed or awk to select the lines you want and extract information from them. There are lots of utilities that select, sort & filter text streams by piping the output of one command into the input of another. Even it you wanted to read in lines of information into a variable, an array would be a better structure to use.

Anyway,

Code:
echo $result |
sed -n '/postfix-mxmf\/smtp/s/.*to=<\([[:alnum:]][[:alnum:]]*@[[:alnum:]][[:alnum:]]*\.[[:alnum:]][[:alnum:]]*\).*low\[\([
[:digit:]][[:digit:]]*\)\])/\1\t\2/p'
user1@domain.com        1484
user1@domain.com        1484
 
Old 07-06-2007, 10:40 AM   #3
Quigi
Member
 
Registered: Mar 2003
Location: Cambridge, MA, USA
Distribution: Ubuntu (Dapper and Heron)
Posts: 377

Rep: Reputation: 31
Please post your question only once. This is a duplicate of http://www.linuxquestions.org/questi...hreadid=567016
 
Old 07-06-2007, 09:48 PM   #4
retrovertigo
Member
 
Registered: Jul 2007
Distribution: Arch Linux
Posts: 36

Original Poster
Rep: Reputation: 15
Yeah, I honestly don't know how it got submitted twice. I'm sorry.

Last edited by retrovertigo; 07-06-2007 at 09:50 PM.
 
Old 07-06-2007, 09:50 PM   #5
retrovertigo
Member
 
Registered: Jul 2007
Distribution: Arch Linux
Posts: 36

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by jschiwal
I don't know why you would want to read a log into a variable. You could use sed or awk to select the lines you want and extract information from them. There are lots of utilities that select, sort & filter text streams by piping the output of one command into the input of another. Even it you wanted to read in lines of information into a variable, an array would be a better structure to use.

Anyway,

Code:
echo $result |
sed -n '/postfix-mxmf\/smtp/s/.*to=<\([[:alnum:]][[:alnum:]]*@[[:alnum:]][[:alnum:]]*\.[[:alnum:]][[:alnum:]]*\).*low\[\([
[:digit:]][[:digit:]]*\)\])/\1\t\2/p'
user1@domain.com        1484
user1@domain.com        1484
I used your method above and got it to work, but then I got to thinking and reworked my script to feed the lines into an array, and the result was much more elegant (and efficient). Thanks for suggesting that!
 
  


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



Similar Threads
Thread Thread Starter Forum Replies Last Post
bash scripting question bmfan Linux - Software 6 11-04-2005 08:54 PM
Bash: Supress newlines with \c - Doesn't work stefanlasiewski Programming 5 07-26-2005 01:17 PM
Split a string on newlines (bash) rose_bud4201 Programming 7 04-14-2005 01:58 PM
BASH Scripting question flagg0204 Programming 4 12-23-2004 07:59 AM
Bash Scripting Question Rezon Programming 2 10-30-2003 10:16 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:24 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