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!

In reply to Re: Replace and substitur in same file by thanos1983
in thread Replace and substitur in same file by Nansh

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



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.