rewrite N lines in Perl

B

Bfons

Hello all,

I am trying to write a script (without much luck) that will modify certain
lines in a sendmail file. Here is what is in the file:

SMailerToTriple=95
R< > $* $@ $1 strip off null relay
R< error : $-.$-.$- : $+ > $* $#error $@ $1.$2.$3 $: $4
R< error : $- : $+ > $* $#error $@ $(dequote $1 $) $: $2
R< error : $+ > $* $#error $: $1
R< local : $* > $* $>CanonLocal < $1 > $2
dnl it is $~[ instead of $- to avoid matches on IPv6 addresses
R< $~[ : $+ @ $+ > $*<$*>$* $# $1 $@ $3 $: $2<@$3> use literal user
R< $~[ : $+ > $* $# $1 $@ $2 $: $3 try qualified mailer
R< $=w > $* $@ $2 delete local host
R< $+ > $* $#_RELAY_ $@ $1 $: $2 use unqualified
mailer

I need it rewritten as:

SMailerToTriple=95
R< > $* $@ $1 strip off null relay
CUSTOM LINE #1
R< error : $- : $+ > $* $#error $@ $(dequote $1 $) $: $2
R< error : $+ > $* $#error $: $1
R< local : $* > $* $>CanonLocal < $1 > $2
dnl it is $~[ instead of $- to avoid matches on IPv6 addresses
R< $~[ : $+ @ $+ > $*<$*>$* $# $1 $@ $3 $: $2<@$3> use literal user
CUSTOM LINE #2
CUSTOM LINE #3
R< $~[ : $+ > $* $# $1 $@ $2 $: $3 try qualified mailer
R< $=w > $* $@ $2 delete local host
R< $+ > $* $#_RELAY_ $@ $1 $: $2 use unqualified
mailer


Can anyone point me in the right direction as I'm having a real hard time
making this fly.

Thank you,

Bfons
 
K

Kris Wempa

Your description is a bit vague. If all you need to do is change line 3 and
add 2 lines after line 8, then just count lines as you read them and take
the appropriate action when line numer = 3 or 8. If there is some other
criteria that you are using to determine where to change/insert lines, then
please explain it.
 
B

Bfons

Kris Wempa said:
Your description is a bit vague. If all you need to do is change line 3 and
add 2 lines after line 8, then just count lines as you read them and take
the appropriate action when line numer = 3 or 8. If there is some other
criteria that you are using to determine where to change/insert lines, then
please explain it.

Each version of Sendmail will have these lines in different places. For
example in the latest version, this ruleset is at line 1294 in the proto.m4
file. In the next version, this ruleset will more than likely be somewhere
else. Up until now, we have been editing it by hand but are tired of doing
this. We need the script to determine where SMailerToTriple=95 is in
proto.m4, and from there make the necessary changes.

Thank you in advance,

Bfons
 
K

Kris Wempa

I'm not sure I completely understand what you need to do. However, if you
just need to identify those lines and change/add custom lines, you could
just use a script such as this:

open(FILE, "< sendmailfile");
opend(NEWFILE, "> newsendmailfile");
while ($line = <FILE>) {

if ($line =~ /^R< error : \$\-\.\$\-\.\$\- : \$\+ > \$\* \$$/) { # if
matched the first line to be changed
print NEWFILE "CUSTOM LINE #1\n";
} else {
if ($line =~ /^R< \$~\[ : \$+ \@ \$\+ > \$\*<\$\*>\$\* \$$/) {
#if matched the second line
print NEWFILE $line; # print the line
print NEWFILE "CUSTOM LINE #2\n"; # add the 2 additional lines
print NEWFILE "CUSTOM LINE #3\n";

} else {
print NEWFILE $line; # if no match, just copy the exact line
}
}
}
close(FILE);
close(NEWFILE);

This will create a new, modified sendmail file from the original one. Is
this what you are trying to do ???
 
B

Bfons

Kris Wempa said:
I'm not sure I completely understand what you need to do. However, if you
just need to identify those lines and change/add custom lines, you could
just use a script such as this:

open(FILE, "< sendmailfile");
opend(NEWFILE, "> newsendmailfile");
while ($line = <FILE>) {

if ($line =~ /^R< error : \$\-\.\$\-\.\$\- : \$\+ > \$\* \$$/) { # if
matched the first line to be changed
print NEWFILE "CUSTOM LINE #1\n";
} else {
if ($line =~ /^R< \$~\[ : \$+ \@ \$\+ > \$\*<\$\*>\$\* \$$/) {
#if matched the second line
print NEWFILE $line; # print the line
print NEWFILE "CUSTOM LINE #2\n"; # add the 2 additional lines
print NEWFILE "CUSTOM LINE #3\n";

} else {
print NEWFILE $line; # if no match, just copy the exact line
}
}
}
close(FILE);
close(NEWFILE);


I need to add the three lines (one on top and two a couple of lines down).
It looks like you gave me something good to go on.

Thank you,

Bfons
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top