http://www.perlmonks.org?node_id=1193193

Nansh has asked for the wisdom of the Perl Monks concerning the following question:

Hi, I have file like this

Eg: I have a Red car

I love Red car

<p But I have a lot of affection with car

#!usr/bin/perl open(DAT,"something/something/something"); while(<DAT>) { $_=~ s/(.*)/Red car/g; print DAT $_; }
I want to Substitute whole file with Red car Thank you

Replies are listed 'Best First'.
Re: Replace and substitur in same file
by GrandFather (Saint) on Jun 21, 2017 at 03:51 UTC

    So how does your current code fail?

    If you add strictures (use strict; use warnings;) do you get information that may help solve your problem?

    Premature optimization is the root of all job security
Re: Replace and substitur in same file
by thanos1983 (Parson) on Jun 21, 2017 at 12:00 UTC

    Hello Nansh,

    Well I am not sure if I understand correctly your question is a bit not cleat to me.

    But if I understand correctly you want to a open a file e.g. data.txt which has some lines inside and then replace these lines with 'Red Car', that's it nothing else?

    If this is the case I would approach it somehow like this (pseudo code):

    #!usr/bin/perl use strict; use warnings; my $infile = '/path/path/data.txt'; open( my $in, "<", $infile ) # use filehandles not bareword or die "Couldn't open $infile: $!"; chomp(my @lines = <$in>); # store data in an array in case you need th +em close $in or warn "close failed: $!"; # close file # empty file # open file in write mode # write your new data on empty file # close file # done :D

    So in further details why to use file handles instead of bareword? read here (open). I quote:

    An older style is to use a bareword as the filehandle, as open(FH, "<", "input.txt") or die "Can't open < input.txt: $!"; Then you can use FH as the filehandle, in close FH and <FH> and so on. + Note that it's a global variable, so this form is not recommended in + new code.

    The point to note here is source Don't Open Files in the old way:

    It is global to all the script you write so if anyone uses the same na +me (IN or OUT in our example) those will clash with yours. It is also harder to pass these variables to functions, than to do the + same with regular scalar variables.

    Then the next step is how to empty the file? Read more about here truncate. But remember once you empty your file there is not way back this is why I copied the content of your file into the array.

    Last step open file in writing mode, read more about it here open it contains all the information on how to do it.

    This is it pretty much :D

    Hope this helps, BR.

    Seeking for Perl wisdom...on the process of learning...not there...yet!

      Thanks it helped me.

Re: Replace and substitur in same file
by Anonymous Monk on Jun 21, 2017 at 05:24 UTC

    Hi,

    Open the source file, checking for success.

    Open a destination file, checking for success.

    Read source file line by line, make substitutions, write to destination file.

    Close files for neatness ( OCD, I can't help myself ).

    Check for success everywhere you can.

    Start EVERY program with strictures. For at least the next 5 years.

    J.C.

      Start EVERY program with strictures. For at least the next 5 years.

      Nah. Forever. The cost is nothing and the benefit, just from spotting typos, way out strips the cost of admission.

      Premature optimization is the root of all job security

        Well, in five years, it should be safe to abandon old versions of Perl, and just:

        use 5.012;

        which is better than use strict because it enables strict mode without needlessly loading a module.