1

I'm trying to make a program that replaces a string in all files in a directory. The catch is that I need to make it possible to only change it if it is at the beginning of a sentence or at the end, or both. This is what I have so far:

use strict;
use warnings;
use File::Find;     #This lets us use the find() function, similar to 'find' in Unix 
                # -- which is especially helpful for searching recursively.

print "Which directory do you want to use?\n"; #Ask what directory to use
my $dir = readline STDIN;
chomp $dir; #Used for eliminating the newline character '\n'

print "What String would you like to search for?\n"; #Ask for what String to search for.
my $search = readline STDIN;
chomp $search;

print "What String would you like to replace it with?\n"; #Ask for what to replace it with.
my $replace = readline STDIN;
chomp $replace;

print "Would you like to replace $search if it is at the beginning of the sentence? (y/n) \n";
my $beg = readline STDIN;
chomp $beg;

print "Would you like to replace $search if it is at the end of the sentence? (y/n) \n";
my $end = readline STDIN;
chomp $end;


find(\&txtrep, $dir); #This function lets us loop through each file in the directory

sub txtrep  {
    if ( -f and /.txt$/) { # Proceeds only if it is a regular .txt file
        my $file = $_;  # Set the name of the file, using special Perl variable
        open (FILE , $file);
        my @lines = <FILE>; #Puts the file into an array and separates sentences
        my @lines2 = split(".", @lines);
        close FILE;

        if ($beg eq "y") {
            foreach my $slot (@lines2) {
                $slot =~ s/^$search/$replace/gi;
            }
        }

        if ($end eq "y") {
            foreach my $slot (@lines2) {
                $slot =~ s/$search$/$replace/gi;
            }
        }

        open (FILE, ">$file");
        print FILE @lines2;
        close FILE;
    }
}

After I run this, it just deletes everything in the files, and I don't know if the syntax is right for changing the string @ the beginning and the end of the sentences. Please let me know what I am doing wrong! Thank you!

2 Answers 2

1

I would just use a perl one-liner

perl -i.bak -pe 's/^[search]/[replace]/g;' [files] for a beginning of a line perl -i.bak -pe 's/[search]$/[replace]/g;' [files] for the end of the line

[search] and [replace] are place holders.

the '-i.bak' will make a backup of the file prior to replacement.

quick google search: perl one-liner search & repalce example

Sign up to request clarification or add additional context in comments.

Comments

0

You have use strict; on, but you have a syntax error with using the variable @lines2.

Also, you should add use autodie;, since you're doing file processing.

The following is probably what you're searching for:

my $data = do {
    open my $fh, $file;
    local $/;
    <$fh>
};

if ($beg eq "y") {
    $data =~ s/(?:^|(?<=\.))\Q$search\E/$replace/gim;
}

if ($end eq "y") {
    $data =~ s/\Q$search\E(?=\.|$)/$replace/gim;
}

open my $fh, '>', $file;
print $fh $data;
close $fh;

Obviously, your code could probably be tightened up more, but the above should get it working.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.