Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Re^2: find & replace a string in perl

by vr786 (Sexton)
on Nov 25, 2010 at 09:25 UTC ( #873604=note: print w/replies, xml ) Need Help??


in reply to Re: find & replace a string in perl
in thread find & replace a string in perl

Hi, here is my modified code, i want to place the comment(#) start of that line my input file consist this data eg: testing is requrired see/for/test test/data/istesting my output should be testing is requrired #see/for/test #test/data/istesting But what ever iam trying it is not working, can you help

#!/usr/bin/perl -w use strict; use warnings; my $line = 0; my $file_name = "test.txt"; open(my $fh, '<', $file_name) || die "file could not open $! \n"; while( $line = <$fh>) { if( $line =~ s/\btest\b/^\#/ig ) { print "$line\n"; } } close($fh);

Replies are listed 'Best First'.
Re^3: find & replace a string in perl
by Corion (Patriarch) on Nov 25, 2010 at 09:29 UTC

    You don't tell us what your input is and what output you get, and how it is different from what you want.

    Again, your regular expression still does not do what you may think it does.

    $line =~ s/\btest\b/^\#/ig

    The right hand side of a regular expression is a simple string.

Re^3: find & replace a string in perl
by mertserger (Curate) on Nov 25, 2010 at 10:10 UTC
    The code:
    if( $line =~ s/\btest\b/^\#/ig ) { print "$line\n"; }
    is definately wrong as Corion has been saying.
    The bit in brackets after the "if" should be a test with a Boolean answer, whereas you've got a substitute command instead. Also if the substitution ever worked you would not be adding a hash in front of the line but replacing "test" with "^#" so your example line "see/for/test " would become "see/for/^#". You want something like:
    if( $line =~ m/test/ ) { $line = "#" . $line; print "$line\n"; }
Re^3: find & replace a string in perl
by Marshall (Canon) on Nov 29, 2010 at 02:40 UTC
    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 $file_name.tm +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

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (3)
As of 2022-06-26 01:10 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    My most frequent journeys are powered by:









    Results (83 votes). Check out past polls.

    Notices?