rewrite N lines in Perl

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Bfons

    rewrite N lines in Perl

    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



  • Kris Wempa

    #2
    Re: rewrite N lines in Perl


    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.


    "Bfons" <linux.rmv@user mail.com> wrote in message
    news:7a114cff6f c0cf2cda8ef6b0f e26f55f@news.te ranews.com...[color=blue]
    > 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[/color]
    relay[color=blue]
    > 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[/color]
    mailer[color=blue]
    > 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[/color]
    relay[color=blue]
    > 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[/color]
    mailer[color=blue]
    > 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
    >
    >
    >[/color]


    Comment

    • Bfons

      #3
      Re: rewrite N lines in Perl


      "Kris Wempa" <calmincents(NO _SPAM)@yahoo.co m> wrote in message
      news:bkpuhv$maa 3@kcweb01.netne ws.att.com...[color=blue]
      >
      > Your description is a bit vague. If all you need to do is change line 3[/color]
      and[color=blue]
      > 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,[/color]
      then[color=blue]
      > please explain it.
      >[/color]

      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


      Comment

      • Kris Wempa

        #4
        Re: rewrite N lines in Perl

        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 ???



        "Bfons" <linux.rmv@user mail.com> wrote in message
        news:ab0ee6b67f 29bf4c0362d1053 eb0f1e4@news.te ranews.com...[color=blue]
        >
        > "Kris Wempa" <calmincents(NO _SPAM)@yahoo.co m> wrote in message
        > news:bkpuhv$maa 3@kcweb01.netne ws.att.com...[color=green]
        > >
        > > Your description is a bit vague. If all you need to do is change line 3[/color]
        > and[color=green]
        > > add 2 lines after line 8, then just count lines as you read them and[/color][/color]
        take[color=blue][color=green]
        > > 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,[/color]
        > then[color=green]
        > > please explain it.
        > >[/color]
        >
        > 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[/color]
        proto.m4[color=blue]
        > file. In the next version, this ruleset will more than likely be[/color]
        somewhere[color=blue]
        > else. Up until now, we have been editing it by hand but are tired of[/color]
        doing[color=blue]
        > 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
        >
        >[/color]


        Comment

        • Bfons

          #5
          Re: rewrite N lines in Perl


          "Kris Wempa" <calmincents(NO _SPAM)@yahoo.co m> wrote in message
          news:bksd27$ma9 5@kcweb01.netne ws.att.com...[color=blue]
          > 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 : \$\-\.\$\-\.\$\- : \$\+ > \$\* \$$/) { #[/color]
          if[color=blue]
          > 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);[/color]


          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


          Comment

          Working...