Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

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;

HTH


Comment on Re: Search and Replace
Download Code
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?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (16)
As of 2015-07-29 14:06 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (263 votes), past polls