Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:
I have a file (called in.txt) that is formatted with field demiliters and text like this: BEGTITLE blah,blah...blah ENDTITLE. The title field is followed by other fields delimiteted the same way. I have been assigned the glorious task of replacing specific words or useless metacharachters only between the title tags, leaving all other instances untouched. I have tried various methods with zero luck (this will probably not be surprising to an experienced PERL programmer!). Here is a sample of my code and INFILE:
My ScriptSample in.txt BEGPUB Wirey Haired Dog's Life ENDPUB BEGTITLE My dog has wirey hair ENDTITLE BEGTXT My wirey hair dog...blah, blah. ENDTXT
I want to replace wirey with smooth in just the title field (which may span multiple lines). My example will replace the word wirey in the other fields as well. I have no basis to believe that my code is anywhere near being correct or useful. I do not even know why I though this could lead anywhere good. Any ideas or suggestions will be much appreciated. Thanks in advance, Stephen#!/usr/bin/perl -w open(INFILE,$ARGV[0]) or die "INFILE CROAKED"; open(OUTFILE,">$ARGV[1]) or die "OUTFILE CROAKED"; While(<INFILE>){ while(/BEGTITLE.*wirey.*?ENDTITLE/){ $_=~s/wirey/smooth/i; print OUTFILE $_;} } close INFILE; close OUTFILE;
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Regex: find/Replace words between tags
by TomK32 (Monk) on Dec 15, 2001 at 07:49 UTC | |
Re: Regex: find/Replace words between tags (non greedy re )
by mandog (Curate) on Dec 15, 2001 at 14:44 UTC | |
by danger (Priest) on Dec 16, 2001 at 02:57 UTC | |
by Anonymous Monk on Dec 16, 2001 at 06:48 UTC | |
by Anonymous Monk on Dec 15, 2001 at 20:51 UTC | |
by Anonymous Monk on Dec 16, 2001 at 02:01 UTC | |
by Anonymous Monk on Dec 16, 2001 at 02:42 UTC | |
Re: Regex: find/Replace words between tags
by dthacker (Deacon) on Dec 15, 2001 at 12:53 UTC |
Back to
Seekers of Perl Wisdom