Edit file in place using perl script

Discussion in 'Perl Misc' started by khan, Oct 17, 2008.

  1. khan

    khan Guest

    Hi,

    I have a requirement where i need to edit the file, based on some
    tokens.
    My requirement is something like this:
    1. If token1 is found delete that line from the file.
    2. If token2 is found replace some part of that line with replacement
    string.
    3. If token3 is found delete that line and next line in the file.

    I wrote a script to do this, but since file-pointer is moved after
    reading it is not editing the file at correct positions.
    Please let me know a solution.

    Thanks,
    -Mushtaq Khan
    khan, Oct 17, 2008
    #1
    1. Advertising

  2. khan <> wrote:
    > Hi,
    >
    > I have a requirement where i need to edit the file, based on some
    > tokens.
    > My requirement is something like this:
    > 1. If token1 is found delete that line from the file.
    > 2. If token2 is found replace some part of that line with replacement
    > string.
    > 3. If token3 is found delete that line and next line in the file.
    >
    > I wrote a script to do this, but since file-pointer is moved after
    > reading it is not editing the file at correct positions.
    > Please let me know a solution.



    You need to modify line 17 in your program.


    --
    Tad McClellan
    email: perl -le "print scalar reverse qq/moc.noitatibaher\100cmdat/"
    Tad J McClellan, Oct 17, 2008
    #2
    1. Advertising

  3. khan <> wrote:
    >I have a requirement where i need to edit the file, based on some
    >tokens.
    >My requirement is something like this:
    >1. If token1 is found delete that line from the file.


    next if index ($_, $token1) >= 0

    >2. If token2 is found replace some part of that line with replacement
    >string.


    s/\Q$somepart\E/$replacementstring/ if index ($_, $token2) >= 0

    >3. If token3 is found delete that line and next line in the file.


    if (index ($_, $token3) >= 0) {
    undef = <>; #skip next line
    next;
    }

    >I wrote a script to do this,


    You have an error on line 42.

    >but since file-pointer is moved after
    >reading it is not editing the file at correct positions.
    >Please let me know a solution.


    perldoc -q "delete a line":
    How do I change one line in a file/delete a line in a file/insert a
    line in the middle of a file/append to the beginning of a file?

    jue
    Jürgen Exner, Oct 17, 2008
    #3
  4. khan

    Tim Greer Guest

    khan wrote:

    > Hi,
    >
    > I have a requirement where i need to edit the file, based on some
    > tokens.
    > My requirement is something like this:
    > 1. If token1 is found delete that line from the file.
    > 2. If token2 is found replace some part of that line with replacement
    > string.
    > 3. If token3 is found delete that line and next line in the file.
    >
    > I wrote a script to do this, but since file-pointer is moved after
    > reading it is not editing the file at correct positions.
    > Please let me know a solution.
    >
    > Thanks,
    > -Mushtaq Khan


    Please show the code from the script that you said you have already
    created to do this, and people can point out the problem with it.
    --
    Tim Greer, CEO/Founder/CTO, BurlyHost.com, Inc.
    Shared Hosting, Reseller Hosting, Dedicated & Semi-Dedicated servers
    and Custom Hosting. 24/7 support, 30 day guarantee, secure servers.
    Industry's most experienced staff! -- Web Hosting With Muscle!
    Tim Greer, Oct 17, 2008
    #4
  5. khan

    smallpond Guest

    On Oct 17, 8:49 am, khan <> wrote:
    > Hi,
    >
    > I have a requirement where i need to edit the file, based on some
    > tokens.
    > My requirement is something like this:
    > 1. If token1 is found delete that line from the file.
    > 2. If token2 is found replace some part of that line with replacement
    > string.
    > 3. If token3 is found delete that line and next line in the file.
    >
    > I wrote a script to do this, but since file-pointer is moved after
    > reading it is not editing the file at correct positions.
    > Please let me know a solution.
    >
    > Thanks,
    > -Mushtaq Khan


    perldoc perlrun

    The -i switch explains how to do this
    1) rename the original file to file.bak
    2) if successful, open a new file for output
    3) if successful, read the original file, and write to the new file
    4) loop through all lines
    5) if successful, unlink file.bak

    Using -i saves writing the file and loop code.
    smallpond, Oct 17, 2008
    #5
  6. khan

    khan Guest

    On Oct 17, 10:37 pm, Tim Greer <> wrote:
    > khan wrote:
    > > Hi,

    >
    > > I have a requirement where i need to edit the file, based on some
    > > tokens.
    > > My requirement is something like this:
    > > 1. If token1 is found delete that line  from the file.
    > > 2. If token2 is found replace some part of that line with replacement
    > > string.
    > > 3. If token3 is found delete that line and next line in the file.

    >
    > > I wrote a script to do this, but since file-pointer is moved after
    > > reading it is not editing the file at correct positions.
    > > Please let me know a solution.

    >
    > > Thanks,
    > > -Mushtaq Khan

    >
    > Please show the code from the script that you said you have already
    > created to do this, and people can point out the problem with it.
    > --
    > Tim Greer, CEO/Founder/CTO, BurlyHost.com, Inc.
    > Shared Hosting, Reseller Hosting, Dedicated & Semi-Dedicated servers
    > and Custom Hosting.  24/7 support, 30 day guarantee, secure servers.
    > Industry's most experienced staff! -- Web Hosting With Muscle!- Hide quoted text -
    >
    > - Show quoted text -


    Hi,
    I want change the file without using the temporary file. Following is
    script but it is not working

    $File = "D:\\PerlProgramming\\Serach-pattren\\file.txt";
    open(FILE, "+<",$File) or die "can't open $File: $!";

    while (<FILE>) {
    if(m/Anand/) {
    $_ =~ s/Football/Cricket/s; #Susbst Football with Cricket
    print FILE $_;
    }
    elsif(m/Jack/) {
    $_ =~ s/^.*\n$//s; #Delete the Line
    print FILE $_;
    }
    }
    close (FILE);
    exit;

    #Input file: file.txt
    Anand Football
    Adam Football
    Jack Tennis
    Jhon Hockey
    #Output with the Above script : file.txt
    Anand Football
    Adam Football
    Jack Tennis
    Jhon Hockey
    Anand Football
    Adam Cricket
    Jhon Hockey

    Script is adding modified data to the end of file, i really didn't
    understand the behaviour of the script.
    #Output what i was expecting : file.txt should have lines
    Anand Football
    Adam Cricket
    Jhon Hockey

    The following script works but it uses a temporary file for storing
    modified data.
    I think this is not an efficient way to do this.

    $File = "D:\\PerlProgramming\\Serach-pattren\\file.txt";
    $FileOut = "D:\\PerlProgramming\\Serach-pattren\\fileout.txt";

    open(FILE, "+<",$File) or die "can't open $File: $!";
    open(OUT, ">",$FileOut) or die "can't open $FileOut: $!";

    while (<FILE>) {
    if(m/Anand/) {
    $_ =~ s/Football/Cricket/s;
    print OUT $_;
    }
    elsif(m/Jack/) {
    $_ =~ s/^.*\n$//s; #Delete the Line
    print OUT $_;
    }
    else {
    print OUT $_;
    }
    }
    close (FILE);
    close (OUT);
    system ("del D:\\PerlProgramming\\Serach-pattren\\file.txt /Q");
    system ("ren D:\\PerlProgramming\\Serach-pattren\\fileout.txt
    file.txt");
    exit
    khan, Oct 20, 2008
    #6
  7. khan wrote:
    >
    > On Oct 17, 10:37 pm, Tim Greer <> wrote:
    >>
    >> khan wrote:
    >>>
    >>> I have a requirement where i need to edit the file, based on some
    >>> tokens.
    >>> My requirement is something like this:
    >>> 1. If token1 is found delete that line from the file.
    >>> 2. If token2 is found replace some part of that line with replacement
    >>> string.
    >>> 3. If token3 is found delete that line and next line in the file.
    >>> I wrote a script to do this, but since file-pointer is moved after
    >>> reading it is not editing the file at correct positions.
    >>> Please let me know a solution.

    >>
    >> Please show the code from the script that you said you have already
    >> created to do this, and people can point out the problem with it.

    >
    > I want change the file without using the temporary file. Following is
    > script but it is not working
    >
    > $File = "D:\\PerlProgramming\\Serach-pattren\\file.txt";
    > open(FILE, "+<",$File) or die "can't open $File: $!";
    >
    > while (<FILE>) {
    > if(m/Anand/) {
    > $_ =~ s/Football/Cricket/s; #Susbst Football with Cricket


    The /s option affects the behaviour of the . character class but you are
    not using . in the pattern so the /s is superfluous.

    > print FILE $_;
    > }
    > elsif(m/Jack/) {
    > $_ =~ s/^.*\n$//s; #Delete the Line
    > print FILE $_;


    If you want to delete the line why are you printing it out?

    > }
    > }
    > close (FILE);
    > exit;


    You are opening the file for input *AND* output ("+<") so you have one
    filehandle and one pointer to the data in the file. When you read the
    first record the pointer moves to the end of that record and then you
    write a record, overwriting the second record in the file and moving the
    pointer to the end of what you have just written, and then the next read
    starts at that point and moves the pointer, and the next write which
    again moves the pointer, etc., etc.


    > #Input file: file.txt
    > Anand Football
    > Adam Football
    > Jack Tennis
    > Jhon Hockey
    > #Output with the Above script : file.txt
    > Anand Football
    > Adam Football
    > Jack Tennis
    > Jhon Hockey
    > Anand Football
    > Adam Cricket
    > Jhon Hockey
    >
    > Script is adding modified data to the end of file, i really didn't
    > understand the behaviour of the script.
    > #Output what i was expecting : file.txt should have lines
    > Anand Football
    > Adam Cricket
    > Jhon Hockey
    >
    > The following script works but it uses a temporary file for storing
    > modified data.
    > I think this is not an efficient way to do this.


    Why do you think that it is not efficient?

    > $File = "D:\\PerlProgramming\\Serach-pattren\\file.txt";
    > $FileOut = "D:\\PerlProgramming\\Serach-pattren\\fileout.txt";
    >
    > open(FILE, "+<",$File) or die "can't open $File: $!";
    > open(OUT, ">",$FileOut) or die "can't open $FileOut: $!";
    >
    > while (<FILE>) {
    > if(m/Anand/) {
    > $_ =~ s/Football/Cricket/s;
    > print OUT $_;
    > }
    > elsif(m/Jack/) {
    > $_ =~ s/^.*\n$//s; #Delete the Line
    > print OUT $_;
    > }
    > else {
    > print OUT $_;
    > }
    > }
    > close (FILE);
    > close (OUT);
    > system ("del D:\\PerlProgramming\\Serach-pattren\\file.txt /Q");


    perldoc -f unlink

    > system ("ren D:\\PerlProgramming\\Serach-pattren\\fileout.txt


    perldoc -f rename

    > file.txt");
    > exit



    John
    --
    Perl isn't a toolbox, but a small machine shop where you
    can special-order certain sorts of tools at low cost and
    in short order. -- Larry Wall
    John W. Krahn, Oct 20, 2008
    #7
  8. khan

    Guest

    khan <> wrote:
    >
    > Hi,
    > I want change the file without using the temporary file.


    I want a million bucks, peace on Earth, and the ability to fly. That is
    unlikely to work out as I want, either.

    You could try Tie::File. It will be slow on big files, as your changes are
    not length-preserving.

    Why not use the temp file? Use the thing that works.

    And see perldoc -f seek, where it discusses switching between reading and
    writing.

    Xho

    --
    -------------------- http://NewsReader.Com/ --------------------
    The costs of publication of this article were defrayed in part by the
    payment of page charges. This article must therefore be hereby marked
    advertisement in accordance with 18 U.S.C. Section 1734 solely to indicate
    this fact.
    , Oct 20, 2008
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. dpackwood
    Replies:
    3
    Views:
    1,792
  2. dba56
    Replies:
    0
    Views:
    592
    dba56
    Jul 7, 2006
  3. Laurent Julliard

    [Q] In place edit mode activation?

    Laurent Julliard, Jul 9, 2008, in forum: Ruby
    Replies:
    1
    Views:
    89
    Joel VanderWerf
    Jul 10, 2008
  4. vikrant
    Replies:
    4
    Views:
    278
  5. BlinKol
    Replies:
    5
    Views:
    156
    BlinKol
    Jan 11, 2006
Loading...

Share This Page