in reply to opening file for editing

For a quick file update, perl -pi -e "s/e/\*\*/ig" D:\RAM\perl\test should work. If your needs are more complex, see open, in particular the '+<' and '+>' bits.

update - changed single to double quotes around the s///ig after belatedly realizing the significance of D:

Replies are listed 'Best First'.
Re^2: opening file for editing
by ramprasad27 (Sexton) on Oct 09, 2011 at 08:10 UTC
    I wont be able to do it using perl one liner what i want is open an already existing file search for a pattern replace it with something else on the fly i dont want any temp files to be created
      Just For Fun: ... to update a file "in place".
      ... open(my $READER,'+<',$Filename_S) or die "Can't open '$Filename_S'! $!"; open(my $WRITER,'+<',$Filename_S) # Don't use + '+>' or die "Can't open '$Filename_S'! $!"; # The overflow buffer: my @Buffer_a; while (<$READER>) { # Read (past tense) a line - buffer its replacement ... # Modify the line here! my $Update_s=... push(@Buffer_a,$Update_s); # Write from the overflow buffer if we can ... while (@Buffer_a && length($Buffer_a[0]) < tell($READER)-tell( +$WRITER)) { # Enough room to write $Buffer_a[0] so write it ... print $WRITER shift(@Buffer_a); }; }; # Nothing more to read ... close($READER) or die "Can't close '$Filename_S'! $!"; # If there's anything in the buffer write it ... while (@Buffer_a) { print $WRITER shift(@Buffer_a); }; # Truncate the file, in case, what we're writing is shorter than w +hat we read truncate($WRITER,tell($WRITER)); # ... and close close($WRITER) or die "Can't close '$Filename_S'! $!"; ...
      Be aware that if something should happen during the update one may be "up the creek without a paddle".

      you can do this in two steps:

      read open dump all to a variable (in memory) apply a substitution to this variable close re-open the file, now for writing print the changed variable to the file (overwrite the whole file) and close again

      No temp files here, and your file is updated as you want. Use perldoc for the details.

        oh well, this seems to be more straightforward solution, thats fine..but can this be done by opening file only once
      Then you'll have to switch to Microsoft Word ™.
        so its not possible using perl