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
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.