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

  • SEARCH
  • Home
  • 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 7182809
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T17:50:56+00:00 2026-05-28T17:50:56+00:00

I am trying to do a dynamic search and replace with Perl on the

  • 0

I am trying to do a dynamic search and replace with Perl on the command line with part of the replacement text being the output of a grep command within backticks. Is this possible to do on the command line, or will I need to write a script to do this?

Here is the command that I thought would do the trick. I thought that Perl would treat the backticks as a command substitution, but instead it just treats the backticks and the content within them as a string:

perl -p -i -e 's/example.xml/http:\/\/exampleURL.net\/`grep -ril "example_needle" *`\/example\/path/g' `grep -ril "example_needle" *`

UPDATE:

Thanks for the helpful answers. Yes, there was a typo in my original one-liner: the target file of grep is supposed to be *.

I wrote a small script based on Schewrn’s example, but am having confusing results. Here is the script I wrote:

 #!/usr/bin/env perl -p -i

my $URL_First = "http://examplesite.net/some/path/";
my $URL_Last = "/example/example.xml";

my @files = `grep -ril $URL_Last .`;
chomp @files;

foreach my $val (@files) {
        @dir_names = split('/',$val);

        if(@dir_names[1] ne $0) {

            my $url = $URL_First .  @dir_names[1] . $URL_Last;

            open INPUT, "+<$val" or die $!;

            seek INPUT,0,0;

            while(<INPUT>) {
                    $_ =~ s{\Q$URL_Last}{$url}g;
                    print INPUT $_;
                    }
            close INPUT;
            }
    }

Basically what I am trying to do is:

  1. Find files that contain $URL_Last.
  2. Replace $URL_Last with $URL_First plus the name of the directory that the matched file is in, plus $URL_Last.
  3. Write the above change to the input file without modifying anything else in the input file.

After running my script, it completely garbled the HTML code in the input file and it cut off the first few characters of each line in the file. This is strange, because I know for sure that $URL_Last only occurs once in each file, so it should only be matched once and replaced once. Is this being caused by a misuse of the seek function?

  • 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-05-28T17:50:57+00:00Added an answer on May 28, 2026 at 5:50 pm

    Everyone’s answers were very helpful to my writing a script that wound up working for me. I actually found a bash script solution yesterday, but wanted to post a Perl answer in case anyone else finds this question through Google.

    The script that @TLP posted at http://codepad.org/BFpIwVtz is an alternative way of doing this.

    Here is what I ended up writing:

    #!/usr/bin/perl
    
    use Tie::File;
    
    my $URL_First = 'http://example.com/foo/bar/';
    my $Search = 'path/example.xml';
    my $URL_Last = '/path/example.xml';
    
    # This grep returns a list of files containing "path/example.xml"
    my @files = `grep -ril $Search .`;
    chomp @files;
    
    foreach my $File_To_Edit (@files) {
    
    # The output of $File_To_Edit looks like this: "./some_path/index.html"
    # I only need the "some_path" part, so I'm going to split up the output and only use @output[1] ("some_path")
        @output = split('/',$File_To_Edit);
    
    # "some_path" is the parent directory of "index.html", so I'll call this "$Parent_Dir"
        my $Parent_Dir = @output[1];
    
    # Make sure that we don't edit the contents of this script by checking that $Parent_Dir doesn't equal our script's file name.
        if($Parent_Dir ne $0) {
    
                # The $File_To_Edit is "./some_path/index.html"
                tie @lines, 'Tie::File', $File_To_Edit or die "Can't read file: $!\n";
                foreach(@lines) {
                        # Finally replace "path/example.xml" with "http://example.com/foo/bar/some_path/path/example.xml" in the $File_To_Edit
                        s{$Search}{$URL_First$Parent_Dir$URL_Last}g;
                        }
                untie @lines;
                }
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to implement a dynamic search jquery and php, but I am
I am trying to create a dynamic advanced search with Search field for name
I am trying to write a dynamic search I and chain where clauses I
I am trying to build some dynamic code that parses a text file with
So I'm trying to optimize a query that does a text search on 1000s
I am trying to search using dynamic finders by two fields which are status
I am trying to create a dynamic table that is being built based on
I am trying to search using dynamic finders for two fields: status and OpenOn
I'm trying to enable dynamic fields in the configuration file for my mapping app,
I am trying to understand dynamic linq and expression trees. Very basically trying to

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.