Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>It seems like what you're trying to do is...</p> <ol> <li>Find a file in a tree which contains a given string.</li> <li>Use that file to build a URL.</li> <li>Replace something in a string with that URL.</li> </ol> <p>You have three parts, and you could jam them together into one regex, but it's much easier to do it in three steps. You won't hate yourself in a week when you need to add to it.</p> <p>The first step is to get the filenames.</p> <pre><code># grep -r needs a directory to search, even if it's just the current one my @files = `grep -ril $search .`; # strip the newlines off the filenames chomp @files; </code></pre> <p>Then you need to decide what to do if you get more than one file from <code>grep</code>. I'll leave that choice up to you, I'm just going to take the first one.</p> <pre><code>my $file = $files[0]; </code></pre> <p>Then build the URL. Easy enough...</p> <pre><code># Put it in a variable so it can be configured my $Site_URL = "http://www.example.com/"; my $url = $Site_URL . $file; </code></pre> <p>To do anything more complicated, you'd use <a href="http://search.cpan.org/perldoc?URI" rel="nofollow">URI</a>.</p> <p>Now the search and replace is trivial.</p> <pre><code># The \Q means meta-characters like . are ignored. Better than # remembering to escape them all. $whatever =~ s{\Qexample.xml}{$url}g; </code></pre> <p>You want to edit files using <code>-p</code> and <code>-i</code>. Fortunately we can emulate that functionality.</p> <pre><code>#!/usr/bin/env perl use strict; use warnings; # never do without these my $Site_URL = "http://www.example.com/"; my $Search = "example-search"; my $To_Replace = "example.xml"; # Set $^I to edit files. With no argument, just show the output # script.pl .bak # saves backup with ".bak" extension $^I = shift; my @files = `grep -ril $Search .`; chomp @files; my $file = $files[0]; my $url = $Site_URL . $file; @ARGV = ($files[0]); # set the file up for editing while (&lt;&gt;) { s{\Q$To_Replace}{$url}g; } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload