editing text file in perl

Discussion in 'Perl Misc' started by king, Jul 3, 2006.

  1. king

    king Guest

    I want to edit a perticular line in a 50 lines text file using perl.

    File handle can be used for this.
    But how can i edit a string present in say 20th line using perl
    script.
    king, Jul 3, 2006
    #1
    1. Advertising

  2. king wrote:
    > I want to edit a perticular line in a 50 lines text file using perl.


    That's a FAQ.

    perldoc -q "change one line"

    --
    Gunnar Hjalmarsson
    Email: http://www.gunnar.cc/cgi-bin/contact.pl
    Gunnar Hjalmarsson, Jul 3, 2006
    #2
    1. Advertising

  3. king wrote:

    > I want to edit a perticular line in a 50 lines text file using perl.
    >
    > File handle can be used for this.
    > But how can i edit a string present in say 20th line using perl
    > script.


    #!perl
    use strict;
    use warnings;
    my $file = 'file.dat';
    my $line_counter = 0;
    my $new_content;

    open my $F, '<', $file || die "Cant open $file: $!";
    flock($F, 1) || die "Cant get LOCK_SH on $file: $!";
    while(<$F>) {
    $line_counter ++;
    if ($line_counter == 20) {
    # do something on 20th line, eg. change it
    $_ = "this is new content of line 20\n";
    }
    $new_content.= $_;
    }
    close $F || die "Cant close $file: $!";

    # report
    print $new_content;

    Hope this helps,

    --
    Bart
    Bart Van der Donck, Jul 3, 2006
    #3
  4. king

    Ian Wilson Guest

    Bart Van der Donck wrote:
    > king wrote:
    >
    >
    >>I want to edit a perticular line in a 50 lines text file using perl.
    >>
    >>File handle can be used for this.
    >>But how can i edit a string present in say 20th line using perl
    >>script.

    >
    >
    > #!perl
    > use strict;
    > use warnings;
    > my $file = 'file.dat';
    > my $line_counter = 0;
    > my $new_content;
    >
    > open my $F, '<', $file || die "Cant open $file: $!";
    > flock($F, 1) || die "Cant get LOCK_SH on $file: $!";
    > while(<$F>) {
    > $line_counter ++;
    > if ($line_counter == 20) {
    > # do something on 20th line, eg. change it
    > $_ = "this is new content of line 20\n";
    > }
    > $new_content.= $_;
    > }
    > close $F || die "Cant close $file: $!";
    >
    > # report
    > print $new_content;
    >


    I prefer to keep simple jobs simple :)
    perl -pi -e 'if ($.==20) {s/^.*$/new content for line 20/;}' file.dat
    or
    perl -pi -e '($. == 20) && s/^.*$/new content for line 20/;' file.dat
    Ian Wilson, Jul 3, 2006
    #4
  5. Ian Wilson wrote:
    > I prefer to keep simple jobs simple :)
    > perl -pi -e 'if ($.==20) {s/^.*$/new content for line 20/;}' file.dat
    > or
    > perl -pi -e '($. == 20) && s/^.*$/new content for line 20/;' file.dat


    'short' ne 'simple'

    --
    Gunnar Hjalmarsson
    Email: http://www.gunnar.cc/cgi-bin/contact.pl
    Gunnar Hjalmarsson, Jul 3, 2006
    #5
  6. Gunnar Hjalmarsson <> wrote:
    > Ian Wilson wrote:
    >> I prefer to keep simple jobs simple :)
    >> perl -pi -e 'if ($.==20) {s/^.*$/new content for line 20/;}' file.dat
    >> or
    >> perl -pi -e '($. == 20) && s/^.*$/new content for line 20/;' file.dat

    >
    > 'short' ne 'simple'



    And attempting to re-answer a Frequently Asked Question is neither.


    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
    Tad McClellan, Jul 3, 2006
    #6
  7. king

    Nikita Naresh

    Joined:
    Oct 16, 2012
    Messages:
    2
    Help me

    When i try to execute the following command (perl -pi -e 'if ($.==20) {s/^.*$/new content for line 20/;}' file.dat), I get this error message.
    "Substitution replacement not terminated at -e line 1."

    Kindly guide me with this.
    Nikita Naresh, Oct 16, 2012
    #7
    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. kelly
    Replies:
    2
    Views:
    25,744
    Jim Gibson
    Jan 3, 2006
  2. Tim Rowe

    Re: editing text file

    Tim Rowe, Jun 25, 2003, in forum: Python
    Replies:
    0
    Views:
    649
    Tim Rowe
    Jun 25, 2003
  3. Replies:
    0
    Views:
    585
  4. Replies:
    3
    Views:
    174
    Sisyphus
    Jun 3, 2005
  5. deostroll
    Replies:
    1
    Views:
    153
    deostroll
    Jan 11, 2008
Loading...

Share This Page