Chad Kellerman wrote:
>
>    I know you can do one a liner that appends a string after line in a
> file:
> ie: perl -p -i -e 's/$oldsting/$1\n$nextline/' file.txt
>
>   But I was doing it in a script:
>
> #!/usr/bin/perl
> use strict;
> my $conf = "/home/bob/my.cnf";
> my $string = "mysqld]";
> my $replace = "bob was here";
>
> open (FILE, "$conf") or die "can not open $conf: $!\n";
> my @file = <FILE>;
> close (FILE);
>
for (my $line = 0; $line <= $#file; $line++)
> {
>     $/ = '[';
>     if ($file[$line] =~ /$string$/)
>     {
>         $file[$line] .= "$replace";
>         last;
>     }
> }
>
open (FILE, ">$conf") or die "can not open $conf: $!\n";
> print FILE @file;
> close (FILE);
>
>
> Then I was thinking, there has got to be a better way of doing this..

Hi Chad.

I hope this helps without too much explanation.

Cheers,

Rob



  use strict;
  use warnings;

  my $conf = "/home/bob/my.cnf";
  my $string = "mysqld]";
  my $replace = "bob was here";

  local @ARGV = $conf;  # pretend $conf was on the command line
  local $^I = '';       # equivalent to -i qualifier

  while (<>) {
    print;              # equivalent to -p qualifier
    print $replace, "\n" if /$string$/;
  }




-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to