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

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
You are making something easy into something hard. First, as you read the input file, output changes to a temporary file. Then save a copy of the original file (rename it to something else) and replace it with the new temporary file. This little "dance" with renaming things is done so that at any time if the program should crash (or maybe system reboots!), it is always possible to get the original data back and re-run the program. As a general rule, do not write modified contents directly back into the original file because there will be a window when no data at all exists! If the data set is something important, that might be a very, very bad thing!

There is no need for a substitute regex here. Nor is there a need for a "/g" global match. Just add a # at the front if the line contains at least one boundary separated token of "test".

Update:I modified the code with another thought...when writing filters like this, it is usually a good idea to have the property that running the filter more than once is "harmless". In this case, I prevent multiple # from showing up at the front of the line by just a little addition to the "if" clause.

#!/usr/bin/perl -w use strict; my $file_name = "test.txt"; open(my $in, '<', $file_name) || die "cannot open $file_name for read + $!"; open(my $out, '>', "$file_name.tmp") || die "cannot open $ +p for write $!"; while( <$in>) { print $out "#" if(/\btest\b/i && !/^#/); #only one # at front of li +ne print $out $_; } close ($in); close ($out); # save copy of original file rename ($file_name,"$file_name.bak") || die "problem with rename $!"; # replace original with the modified version rename ("$file_name.tmp", $file_name) || die "problem with rename $!"; = file test.txt before running program testing is requrired see/for/test test/data/istesting = file test.txt after running program testing is requrired #see/for/test #test/data/istesting

In reply to Re^3: find & replace a string in perl by Marshall
in thread find & replace a string in perl by vr786

Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":

  • 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.
Log In?

What's my password?
Create A New User
Domain Nodelet?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others scrutinizing the Monastery: (3)
As of 2024-04-20 19:12 GMT
Find Nodes?
    Voting Booth?

    No recent polls found