Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8511737
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T04:05:21+00:00 2026-06-11T04:05:21+00:00

I’m new with perl. saw many samples but had problems composing a solution I

  • 0

I’m new with perl. saw many samples but had problems composing a solution
I have a list of strings which each string should be replaced in a different string a->a2, b->b34, etc. list of replacement is in some csv file. need to perform this replacement recursively on all files in directory.
might be any other language just thought perl would be the quickest

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T04:05:23+00:00Added an answer on June 11, 2026 at 4:05 am

    Your problem can be split into three steps:

    1. Getting the search-and-replace strings from the CSV file,
    2. Getting a list of all text files inside a given directory incl. subdirectories, and
    3. Replacing all occurences of the search strings with their replacements.

    So lets do a countdown and see how we can do that 🙂

    #!/usr/bin/perl
    use strict; use warnings;
    

    3. Search and replace

    We will define a sub searchAndReplace. It takes a file name as argument and accesses an outside hash. We will call this hash %replacements. Each key is a string we want to replace, and the value is the replacement. This “imposes” the restriction that there can only be one replacement per search string, but that should seem natural. I will further assume that each file is reasonably small (i.e. fits into RAM).

    sub searchAndReplace {
      my ($filename) = @_;
      my $content = do {
        open my $file, "<", $filename or die "Cant open $filename: $!";
        local $/ = undef; # set slurp mode
        <$file>;
      };
      while(my ($string, $replacement) = each %replacements) {
        $content =~ s/\Q$string\E/$replacement/g;
      }
      open my $file, ">", $filename or die "Can't open $filename: $!";
      print $file $content; # I didn't forget the comma
      close $file;
    }
    

    This code is pretty straightforward, I escape the $string inside the regex so that the contents aren’t treated as a pattern. This implementation has the side effect of possibly replacing part of the $content string where something already was replaced, but one could work around that if this is absolutely neccessary.

    2. Traversing the file tree

    We will define a sub called anakinFileWalker. It takes a filename or a name of an directory and the searchAndReplace sub as arguments. If the filename argument is a plain file, it does the searchAndReplace, if it is a directory, it opens the directory and calls itself on each entry.

    sub anakinFileWalker {
      my ($filename, $action) = @_;
      if (-d $filename) {
        opendir my $dir, $filename or die "Can't open $filename: $!";
        while (defined(my $entry = readdir $dir)) {
          next if $entry eq '.' or $entry eq '..';
          # come to the dark side of recursion
          anakinFileWalker("$filename/$entry", $action); # be sure to give full path
        }
      } else {
        # Houston, we have a plain file:
        $action->($filename);
      }
    }
    

    Of course, this sub blows up if you have looping symlinks.

    1. Setting up the %replacements

    There is a nice module Text::CSV which will help you with all your needs. Just make sure that the %replacements meet the definition above, but that isn’t hard.

    Starting it all

    When the %replacements are ready, we just do

    anakinFileWalker($topDirectory, \&searchAndReplace);
    

    and it should work. If not, this should have given you an idea about how to solve such a problem.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm new to using the Perl treebuilder module for HTML parsing and can't figure
I want to count how many characters a certain string has in PHP, but
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have a French site that I want to parse, but am running into
This could be a duplicate question, but I have no idea what search terms
I have an autohotkey script which looks up a word in a bilingual dictionary
I have a text area in my form which accepts all possible characters from
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
I have a jquery bug and I've been looking for hours now, I can't
link Im having trouble converting the html entites into html characters, (&# 8217;) i

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.