Bash scripting question re: newlines


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Bash scripting question re: newlines
# 1  
Old 07-05-2007
Bash scripting question re: newlines

I'm writing a bash script to search the contents of a postfix log. To keep the script's output readable (since multiple lines from the log file need to be echo'ed) I am setting the IFS variable to an empty string so that the line breaks in my grep results are preserved. I am storing the results to a variable. Later in the script, I want to search the entire output as a single line of text. I am not sure if it is possible to make sed consider the results in this variable as a single line, so I am trying to simply remove all the line breaks using the following:

Code:
str1=${results//\n/}

However, it is not working. Does anyone have an idea what I'm doing wrong? The reason that I am trying to search the results as a single line is because I'm looping through all the lines in the search results, searching for a pattern that occurs multiple times. Each time a match is found, I do some further processing. However, if I just run the search results through sed, I get a list of every match. I'd like to be able to process each line separately if possible, but since I haven't been able to figure out how to do that I was trying to treat it as one long line and just parse through it from beginning to end.

If anyone has a better idea on how to do this, I'm open to suggestions. I'm intermediate when it comes to shell scripting, but by no means an expert like so much of you here. Smilie
# 2  
Old 07-05-2007
Retrovertigo,
Please show us a sample of your input file and expected output.
# 3  
Old 07-05-2007
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

Keep in mind that these results are stored in a variable called "results", and the IFS variable has been set to an empty string so that, when echo'ed, the above results will display with line breaks intact.

I want to 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 is that, for each "postfix-mxmf/smtp" line, I grep another 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


Thanks for the help!

Last edited by retrovertigo; 07-05-2007 at 06:44 PM..
# 4  
Old 07-06-2007
You don't need to modify IFS :
Code:
$ var='aaaaaaaaaaaaaaa
> bbbbbbbbbbbbbbbbbbbb
> ccccccccccccccc'
$ echo $var
aaaaaaaaaaaaaaa bbbbbbbbbbbbbbbbbbbb ccccccccccccccc
$ echo "$var"
aaaaaaaaaaaaaaa
bbbbbbbbbbbbbbbbbbbb
ccccccccccccccc
$

You can do something like that to filter lines :
Code:
var=$(<input_logfile)
echo "$var" |
sed -n '/postfix-mxmf\/smtp/{s/^.*to=<\([^>]*\)>.*low\[\([^[]*\)].*$/\1 \2/;p;}' |
while read address low
do
   echo  "Adress=$address  Low=$low"
done

Jean-Pierre.
# 5  
Old 07-06-2007
Cool, thanks!
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Total Noob BASH scripting question

Hello All, I have a file of ip addresses called activeips.txt What I'm trying to do is run a simple bash script that has a loop in it. The loop is a cat of the IP addresses in the file. The goal is to run 2 nmap commands to give me outputs where each address in the list has an OS... (11 Replies)
Discussion started by: Dirk_Pitt
11 Replies

2. Shell Programming and Scripting

Beginner Bash Scripting Question

Hello, I am new to Linux and studying to become a Unix System Admin. I am taking a course in which I was practicing creating a bash script to ping a particular IP address. The script can be found below: #/bin/bash echo "Enter the IP address" read ip if then ping -c 1 $ip if ;... (3 Replies)
Discussion started by: shah9250
3 Replies

3. Shell Programming and Scripting

bash scripting

same script: 1- i am using grep to find a string called: tinker panic 0 in a file /etc/ntp.conf if the string is not there, i want to add the strings in /etc/ntp.conf file in the first line of the file. if not do nothing or exit. 2- also i want to add # in front of the following lines in... (0 Replies)
Discussion started by: lamoul
0 Replies

4. Shell Programming and Scripting

Bash scripting question

have a script that calls child scripts depending on conditions. All of the child scripts source in a common file that contains shared functions. At the moment each script has to source this file itself, is there a way for the master script to automagically source the file for them? For... (3 Replies)
Discussion started by: redman
3 Replies

5. Shell Programming and Scripting

Preserving newlines when writing loops on the command line in bash

Dear All, I have a question that's been difficult to get an answer to. I often write command line loops, e.g. find files, print name, grep for term, apply sed, etc I use both zsh and bash. When I write a loop e.g. for line in `more myfile.txt` > do > echo $line > done but... (2 Replies)
Discussion started by: JohnK1
2 Replies

6. Shell Programming and Scripting

Bash scripting, question about a variable

Hey, So I've run into a problem, due to my limited knowledge of Bash scripting. Basically I've got a long script and I want to understand it before I even try and edit it. As long as I don't understand the script, I will not bother editing it. Anyway, the following variable confuses me... (5 Replies)
Discussion started by: abciscool
5 Replies

7. Homework & Coursework Questions

bash,scripting

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted! 1. The problem statement, all variables and given/known data: i have to do this but i am confused, Create a file containing the bash functions which perform the... (1 Reply)
Discussion started by: CRAZYLITTLELOU
1 Replies

8. Shell Programming and Scripting

bash scripting help!!

Hi, can anyone help me with my scrip please. I wanted do following tasks: 1. List all the directory 2. A STDIN to ask user to enter a directory name from listed directories 3. command to check if the directory exists( or a command to validate if the user entered a valid directory name)... (3 Replies)
Discussion started by: eminjan
3 Replies

9. Shell Programming and Scripting

bash scripting help

hi all i'm trying to get a script working upon connection with pppd According to docu this happens ina clean environment with a couple of variables set, namely $1,$2,... To be able to execute the statements i included a path statement but i think i'm running into trouble with the variables -... (6 Replies)
Discussion started by: jimjones
6 Replies
Login or Register to Ask a Question