Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Search & replace problem 2

Status
Not open for further replies.

nogs

Technical User
Aug 3, 2001
89
GB
This script is now doing my head in, Im getting no errors when running it and I know it is reading the file I specified it just isnt replacing the text.
Anyone have any ideas before I totally loose it.
Thanks for any help in advance

#!\perl\bin -
print 'What is your the file (complete path required) you are looking for?';
$filename= <STDIN>;
chomp ($filename);
if (open (EXAMPLE, &quot;$filename&quot;))
{
print &quot;what text would you like to replace? \n&quot;;
$search= <STDIN>;
chomp ($search);
print &quot;what text would you like in its place? \n&quot;;
$replace= <STDIN>;
chomp ($replace);
$count = 0;

$text = <EXAMPLE>;
while ($text =~ s/$search/$replace/)
{
$count++;
$text = <EXAMPLE>;
}
print &quot;count = $count \n&quot;;
}
else
{
print &quot;That file does not exist\n&quot;;
}
 
In order to change the contents of a file, you need to do the following steps.
1) open file for reading
2) read file
3) change file contents
4) write changed file contents to the file

You have done steps 1-3, but not 4. Here's some code that should do the trick.
Code:
print &quot;What is your the file (complete path required) you are looking for &quot;;
$filename= <STDIN>;
chomp ($filename);
# open file for reading
unless (open (EXAMPLE, &quot;$filename&quot;)) {
    print &quot;That file does not exist\n&quot;;
    exit(1);
}
print &quot;what text would you like to replace? &quot;;
chomp($search = <STDIN>);
print &quot;what text would you like in its place? &quot;;
chomp($replace= <STDIN>);
undef($/); # Undefine input record separator so entire file can be read into a string
$text = <EXAMPLE>;
close(EXAMPLE);
$/ = &quot;\n&quot;; # Set input record separator back to default
$count = $text =~ s/$search/$replace/g; # global change - number of changes returned to $count
# open file rot writing
unless (open (EXAMPLE, &quot;>$filename&quot;)) {
    print &quot;Error opening $filename: $!\n&quot;;
    exit(1);
}
print EXAMPLE $text;
print &quot;Replaced '$search' with '$replace' $count times \n&quot;;
 
raider2001
Thanks for spotting my stupid error!!
That works spot on, how would be the best way of getting it to change the records 1 at a time and prompt user if they would like to change another 1???

Nogs[ponder]
 
I've seen that same error on several threads so don't worry about it. The following code will allow multiple files and multiple records to be updated 1 at a time.
Code:
while (1) {
    print &quot;What is your the file (complete path required) you are looking for (q to quit) &quot;;
    chomp($filename= <STDIN>);
    last if ($filename eq &quot;q&quot;);
    # open file for reading
    unless (open (EXAMPLE, &quot;$filename&quot;)) {
        print &quot;That file does not exist\n&quot;;
        next;
    }
    undef($/); # Undefine input record separator so entire file can be read into a string
    $text = <EXAMPLE>;
    close(EXAMPLE);
    $/ = &quot;\n&quot;; # Set input record separator back to default
    while(1) {
        print &quot;what text would you like to replace? (q to quit) &quot;;
        chomp($search = <STDIN>);
        last if ($search eq &quot;q&quot;);
        print &quot;what text would you like in its place? &quot;;
        chomp($replace= <STDIN>);
        $count = $text =~ s/$search/$replace/g; # global change - number of changes returned to $count
        $count = 0 if ($count eq &quot;&quot;);
        print &quot;Replaced '$search' with '$replace' $count times \n&quot;;
    }
    # open file for writing
    unless (open (EXAMPLE, &quot;>$filename&quot;)) {
        print &quot;Error opening $filename: $!\n&quot;;
        exit(1);
    }
    print EXAMPLE $text;
    close(EXAMPLE);
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top