alpal has asked for the wisdom of the Perl Monks concerning the following question:


First off, I'm sure you can tell by my code that I'm pretty new to this. The goal here is to search through thousands of log files (can be stored as *.txt or *.log) and find a particular key (in this case the name of a user) and replace every instance of that name in all of the files with a different username.

This is what I have so far (I've looked around a lot at examples of code so you might recognize some pieces of this)...

print "\nEnter extension of files to look in (example: log or tx +t):\n\n"; $fileext = <STDIN>; $fileext = lc($fileext); print "\nConfirmed: $fileext\n\n"; print "\nEnter exact name of player to find and replace:\n\n"; $playertofind = <STDIN>; print "\nConfirmed: $playertofind\n\n"; print "\nEnter exact name of player to replace $playertofind with:\n\n +"; $newplayer = <STDIN>; print "\nConfirmed: $newplayer\n\n"; @filestocheck = <*.$fileext>; foreach $file (@filestocheck) { open FILE, "$file" or die "\nError: Unable to open file for read. +.."; @lines = <FILE>; close FILE; open FILE, ">$file" or die "\nError: Unable to open file for writ +e..."; foreach $line (@lines) { $line =~ s/$playertofind/$newplayer/g; print FILE $line; } close FILE; }


No editing takes place when this is run, but there is something unusual. If I replace the variables $playertofind and $newplayer with straight text, everything works pretty well. There's a similar problem with the @filestocheck = <*.$fileext>;

I can't imagine it taking very long for an experienced person to figure this out. Me on the other hand...I obviously can't figure it out! Ha!

Thanks for your help!
Alan

Replies are listed 'Best First'.
Re: Simple file editing problems
by kabel (Chaplain) on Oct 23, 2002 at 20:35 UTC
    in nearly every die () call: print out the $! scalar, which holds the "true" system error message.

    please put the following two lines above your scripts:
    use strict; use warnings;
    explanation can be found here

    this code example works for me:
    use strict; use warnings; open (FILE, "<test.data") or die "open: $!"; my @file_contents = <FILE>; close (FILE); @file_contents = map {s/stefan/kabel/g; $_;} @file_contents; open (FILE, ">test.data") or die "open 2: $!"; print FILE $_ foreach (@file_contents); close (FILE);
    not the nicest code, but who cares.
Re: Simple file editing problems
by fglock (Vicar) on Oct 23, 2002 at 20:25 UTC

    Use  chomp in the input variables, in order to remove the "newline" or "enter" character.

    for example:

    $fileext = <STDIN>; chomp $fileext;
Re: Simple file editing problems
by Wonko the sane (Curate) on Oct 24, 2002 at 17:31 UTC

    I Beleive that fglock is right, that your problem is that the variables in your regex($playertofind, $newplayer, $fileext), still have a newline on the end, so they dont match.

    A quick way of doing this from the command line:

    perl -pi.orig -e 's/Wonko/Bilbo Baggins/g' *.txt *.log

    Found in "Perl Cookbook, CH 7" and in "Programming Perl"

    Best Regards,
    Wonko