perl scriipt - replace a string in every file in a directory

All forums need a place for all the posts for people in the community to rant about things not relating to what the forum is about. This is that forum.

Moderator: MacroQuest Developers

BardsAreTooEasy
a lesser mummy
a lesser mummy
Posts: 39
Joined: Sun Feb 27, 2005 3:01 pm

perl scriipt - replace a string in every file in a directory

Post by BardsAreTooEasy » Thu Aug 04, 2005 9:34 pm

I wrote this perl script when String TLO was removed. It will replace a string with a new string in every file in a directory, and uses perl regex. Here's how I would use it to replace ${String}'s in my entire Macro directory. Don't worry, this will not destroy your origional files, it will make all new files called $OLDFILE.new.

./replace
Enter the origional string:
..String.(.*)[}]](.*)
Enter the replacement string:
$1$2

This will replace (${String[${Me.Name}].Equal[Ben]}) with (${Me.Name.Equal[Ben]}) and ${String[${Target.Name}].Equal[Wolf]} to ${Target.Name.Equal[Wolf]}.

this script is writin for unix like systems, works fine with cygwin though, and I use it for tons of stuff, especially maintaining my web scripts. Here's the code:

Code: Select all


#!/usr/bin/perl
use strict;
use warnings;
use diagnostics;
my $directory = `ls`;
if($directory !~ /replace_temp/) {
        system("mkdir replace_temp");
}
print "Enter origional string: \n";
my $ostring = <STDIN>;
chomp($ostring);
print "Enter replacement string: \n";
my $nstring = <STDIN>;
chomp($nstring);
my @cat = `grep -P -H -m 1 "$ostring" *`;
foreach my $catline (@cat) {
        (my $file, my $matching_strings) = split(/:/, $catline);
        system("cp $file replace_temp\/");
}

#replace ..String.(.*)[}]](.*) with $1$2
my @ls = `ls replace_temp`;
chomp(@ls);
foreach my $dir (@ls) {
        if($dir !~ /replace/ && $dir !~ /.new/) {
                open(DIR, "$dir") || die "Can't open $dir";
                open(NEWFILE, ">$dir.new") || die "Can't open $dir NewDir";
                while(my $line = <DIR>) {
                        $line =~ s/$ostring/qq("$nstring")/gee;
                        print NEWFILE "$line";
                }
        close(DIR);
        close(NEWFILE);
        }
}
system("rm -rf replace_temp");



ksmith
orc pawn
orc pawn
Posts: 20
Joined: Mon Feb 09, 2004 1:15 pm
Contact:

Post by ksmith » Fri Aug 26, 2005 12:46 pm

Pssst. you re-invented the wheel.

Code: Select all

perl -spi.bak -e 's/foo/bar/g' list of files

xyilla
naggy
naggy
Posts: 33673
Joined: Sun Feb 23, 2025 5:36 am

Re: perl scriipt - replace a string in every file in a directory

Post by xyilla » Sat Jun 07, 2025 12:16 am


xyilla
naggy
naggy
Posts: 33673
Joined: Sun Feb 23, 2025 5:36 am

Re: perl scriipt - replace a string in every file in a directory

Post by xyilla » Sat Jun 07, 2025 12:18 am


xyilla
naggy
naggy
Posts: 33673
Joined: Sun Feb 23, 2025 5:36 am

Re: perl scriipt - replace a string in every file in a directory

Post by xyilla » Sat Jun 07, 2025 12:19 am


xyilla
naggy
naggy
Posts: 33673
Joined: Sun Feb 23, 2025 5:36 am

Re: perl scriipt - replace a string in every file in a directory

Post by xyilla » Sat Jun 07, 2025 12:20 am


xyilla
naggy
naggy
Posts: 33673
Joined: Sun Feb 23, 2025 5:36 am

Re: perl scriipt - replace a string in every file in a directory

Post by xyilla » Sat Jun 07, 2025 12:21 am


xyilla
naggy
naggy
Posts: 33673
Joined: Sun Feb 23, 2025 5:36 am

Re: perl scriipt - replace a string in every file in a directory

Post by xyilla » Sat Jun 07, 2025 12:22 am


xyilla
naggy
naggy
Posts: 33673
Joined: Sun Feb 23, 2025 5:36 am

Re: perl scriipt - replace a string in every file in a directory

Post by xyilla » Sat Jun 07, 2025 12:23 am


xyilla
naggy
naggy
Posts: 33673
Joined: Sun Feb 23, 2025 5:36 am

Re: perl scriipt - replace a string in every file in a directory

Post by xyilla » Sat Jun 07, 2025 2:11 am


xyilla
naggy
naggy
Posts: 33673
Joined: Sun Feb 23, 2025 5:36 am

Re: perl scriipt - replace a string in every file in a directory

Post by xyilla » Sat Jun 07, 2025 2:12 am


xyilla
naggy
naggy
Posts: 33673
Joined: Sun Feb 23, 2025 5:36 am

Re: perl scriipt - replace a string in every file in a directory

Post by xyilla » Sat Jun 07, 2025 2:14 am


xyilla
naggy
naggy
Posts: 33673
Joined: Sun Feb 23, 2025 5:36 am

Re: perl scriipt - replace a string in every file in a directory

Post by xyilla » Sat Jun 07, 2025 2:15 am


xyilla
naggy
naggy
Posts: 33673
Joined: Sun Feb 23, 2025 5:36 am

Re: perl scriipt - replace a string in every file in a directory

Post by xyilla » Sat Jun 07, 2025 2:16 am


xyilla
naggy
naggy
Posts: 33673
Joined: Sun Feb 23, 2025 5:36 am

Re: perl scriipt - replace a string in every file in a directory

Post by xyilla » Sat Jun 07, 2025 2:17 am