PyrexKidd has asked for the wisdom of the Perl Monks concerning the following question:
how do I modify this code?#!/usr/bin/perl use strict; use warnings; my($copy) = "$ARGV[0].bak"; copy($ARGV[0], $copy) or die "File cannot be copied. \n"; open(INPUT,"$copy") or die 'Cannot open file: $!\n'; open(OUTPUT,">$ARGV[0]"); my($replacePattern1) = "foo1"; my($searchPattern1) = "bar1"; my($replacePattern2) = "foo2"; my($searchPattern2) = "bar2"; my($replacePattern3) = "foo3"; my($searchPattern3) = "bar3"; my($pattern) = 0; while (<INPUT>) { if ($_ =~ s/$searchPattern1/$replacePattern1/g) { $pattern = 1; } elsif ($_ =~ s/$searchPattern2/$replacePattern2/g) { $pattern = 2; } elsif ($_ =~ s/$searchPattern3/$replacePattern3/g) { $pattern = 3; } print OUTPUT $_; }close INPUT; close OUTPUT; unlink($copy);
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Find and Replace from File List
by toolic (Bishop) on May 28, 2010 at 01:08 UTC | |
|
Re: Find and Replace from File List
by dineed (Scribe) on May 28, 2010 at 06:24 UTC | |
by Marshall (Canon) on May 31, 2010 at 17:03 UTC | |
by aquarium (Curate) on May 31, 2010 at 06:56 UTC | |
|
Re: Find and Replace from File List
by Marshall (Canon) on May 28, 2010 at 03:25 UTC | |
by PyrexKidd (Monk) on May 28, 2010 at 15:48 UTC | |
|
Re: Find and Replace from File List
by aquarium (Curate) on May 28, 2010 at 03:32 UTC | |
|
Re: Find and Replace from File List
by baxy77bax (Deacon) on May 28, 2010 at 10:19 UTC |