Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw

Re: Search and Replace

by cchampion (Curate)
on Aug 27, 2003 at 12:25 UTC ( #287018=note: print w/replies, xml ) Need Help??

in reply to Search and Replace

This script will do what you want, provided that your file is not too big for your memory.

#!/usr/bin/perl -w use strict; my $start_flag = '<REANALYZED>'; my $stop_flag = '</REANALYZED>'; my $replacement = <<'REPL'; "z:\path\newfilename1.ext" GL "z:\path\newfilename2.ext" GL "z:\path\newfilename3.ext" GL "z:\path\newfilename4.ext" GL "z:\path\newfilename5.ext" GL "z:\path\newfilename6.ext" GL REPL open ORIGINAL, "< original.txt" or die "can't open original file\n"; my $original; { local $/; $original = <ORIGINAL>; } close ORIGINAL; $original =~ s/ $start_flag .*? (\s*) $stop_flag \n /$start_flag\n$replacement$1$stop_flag/gsx; open COPY, "> copy.txt" or die "can't write to copy\n"; print COPY $original; close COPY;


Replies are listed 'Best First'.
Re: Re: Search and Replace
by dbwiz (Curate) on Aug 27, 2003 at 14:47 UTC

    What about a one-liner?

    If you have your replacement strings in "repl.txt", this will do the trick.

    perl -0pe 'BEGIN{open R,"repl.txt";$repl=<R>;close R}s/(<REANALYZED>). +*?(<\/REANALYZED>)/$1$repl$2/s' original.txt

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://287018]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (3)
As of 2022-01-16 10:15 GMT
Find Nodes?
    Voting Booth?
    In 2022, my preferred method to securely store passwords is:

    Results (49 votes). Check out past polls.