Hello all,
I'm new to perl and currenlty having an issue with a little script. I'm trying to match a variable against another variable read in from a line in a text file and replace based upon a positive match. However, I can never get the match sequence to work correclty.
#!/usr/bin/perl
print "Directory path? ";
chop ($_dirpath = <STDIN>);
print "File extension? ";
chop ($_filext = <STDIN>);
print "Replace: ";
chop ($_match = <STDIN>);
print "By: ";
chop ($_newline = <STDIN>);
opendir (DH,$_dirpath) or die "Can't access directory\n";
while ($olditem = $item = readdir DH)
{
if ($item !~ /\.$_filext$/)
{
print "Skipping $item\n";
next;
}
$olditem =~ s/\.$_filext$/.bak/;
rename "$_dirpath/$item","$_dirpath/$olditem";
open (workinfile,"$_dirpath/$olditem")or die "Can't read Work
+File\n";
open (newfile, ">$_dirpath/$item")or die "Can't read new File\
+n";
while ($_linein = <workinfile>)
{
if ($_linein eq $_match)
{
printf newfile $_linein, "\n" or die "Can't write File\
+n";
printf newfile $_newline, "\n" or die "Can't write File
+\n";
}
else
{
printf newfile $_linein, "\n" or die "Can't write File
+\n";
}
next;
}
close (workinfile);
close (newfile);
}
Any help would be greatly appreciated!!!!!!!!
Thanks In Advance!
-Steve
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.