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:
Sample in.txt
BEGPUB Wirey Haired Dog's Life ENDPUB
BEGTITLE My dog has wirey hair ENDTITLE
BEGTXT My wirey hair dog...blah, blah. ENDTXT
My Script
#!/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;
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
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.
|