FAQ
Hi all,

I am trying to edit the text file using perl script.
i want to replace a particular text of file with new text.

but the problem is that the new text appeared at end of file;

How can i solve this problem,
pls. help........


Regards,
Alpesh.

Search Discussions

  • Chas. Owens at Jul 7, 2009 at 6:36 am

    On Tue, Jul 7, 2009 at 02:25, Alpesh Naikwrote:
    Hi all,

    I  am trying to edit the text file using perl script.
    i want to replace a particular text of file with new text.

    but the problem is that the new text appeared at end of file;

    How can i solve this problem,
    pls. help........


    Regards,
    Alpesh.
    It sounds like you want [in-place editing][0]. You may also want to
    look at the [Tie::File][1] module.
    e

    [0] : http://perldoc.perl.org/perlrun.html#*-i*[_extension_]
    [1] : http://perldoc.perl.org/Tie/File.html

    --
    Chas. Owens
    wonkden.net
    The most important skill a programmer can have is the ability to read.
  • Thomas Bätzler at Jul 7, 2009 at 6:48 am

    Alpesh Naik asked:
    I am trying to edit the text file using perl script.
    i want to replace a particular text of file with new text.

    but the problem is that the new text appeared at end of file;

    How can i solve this problem,
    pls. help........
    We can help you much better if we can actually see what you're doing, so please include sample code.

    A working pattern for modifying a file goes like this:

    1. open old file or reading

    2. open temporary file for writing

    3. read a line from the old file

    4. if the line matches your criteria, then modify the line

    5. write the line to the temporary file

    6. If you have not reached the end of the old file yet, continue at 3.

    7. close the old file

    8. close the temporary file

    9. rename the old file to a temporary filename

    10. rename the temporary file to the name of the old file

    11. optionally, delete the old file

    It's not an in-place edit, but it works. If you need to do in-place edits, you should look at seek() and understand the constraints of doing an in-place edit.

    HTH,
    Thomas
  • Thomas Bätzler at Jul 7, 2009 at 9:07 am

    Alpesh Naik wrote:

    Hi Thomas,
    and thanks for the reply,

    Given below is my code,

    #!/usr/bin/perl
    #!/usr/bin/perl -w

    => You should always enable warnings.
    use strict;

    print "Content-type:text/html\n\n";
    use CGI;
    my $query = new CGI;
    => If you're using CGI, then you should use it everywhere.

    use CGI;

    my $query = new CGI;

    print $query->header() .
    $query->start_html();
    print "Name = ". $query->param('txtname')."<br>";
    print "No = ". $query->param('txtno')."<br>";
    print "Address = ". $query->param('txtaddress')."<br>";
    print "Designation = ". $query->param('txtdesig')."<br>";
    my %cginame = ( 'Name' => 'txtname', 'No' => 'txtno', 'Address' => 'txtaddress', 'Designation' => 'txtdesig' );
    my @fieldorder = qw( Name No Address Designation );

    print $query->start_p;

    foreach my $field (@fieldorder){
    print $query->br( "$field = " . $query->param( $cginame{ $field } ) );
    }

    print $query->end_p;
    open (FILE, "+<data.txt") or die $!;

    while (<FILE>)
    {
    if ($_ =~ m/^\s*+(name|no|address|Designation)/i)
    {
    our    $i = index($_,'=')+2;
    $l = substr $_, $i;
    push (@data,$l);
    }
    }
    => this looks more like C than Perl. Please have a look at split().

    HTH,
    Thomas

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupbeginners @
categoriesperl
postedJul 7, '09 at 6:25a
activeJul 7, '09 at 9:07a
posts4
users3
websiteperl.org

People

Translate

site design / logo © 2022 Grokbase