Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight

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
Node Status?
node history
Node Type: note [id://287018]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (6)
As of 2018-07-16 22:48 GMT
Find Nodes?
    Voting Booth?
    It has been suggested to rename Perl 6 in order to boost its marketing potential. Which name would you prefer?

    Results (352 votes). Check out past polls.