FAQ
Hi all,

I have a file that looks like:

*/tmp/dst/file1*234*RW*6790
*/tmp/dst/file2*568*RW*908
*/tmp/dst/file3*345*RW*746
*/test/flm/file4*354*RW*987
*/test/flm/file5*643*RW*645


I need to keep all of the lines with the exception of the one with
file1 and file3 which have in common "/tmp/dst". I need to preserve
the line /tmp/dst/file2 plus the others that start with */test

Any ideas on how to do this with perl?

Thanks,

BTNA

Search Discussions

  • Gunnar Hjalmarsson at Mar 26, 2008 at 11:53 pm

    btna wrote:
    I have a file that looks like:

    */tmp/dst/file1*234*RW*6790
    */tmp/dst/file2*568*RW*908
    */tmp/dst/file3*345*RW*746
    */test/flm/file4*354*RW*987
    */test/flm/file5*643*RW*645

    I need to keep all of the lines with the exception of the one with
    file1 and file3 which have in common "/tmp/dst". I need to preserve
    the line /tmp/dst/file2 plus the others that start with */test

    Any ideas on how to do this with perl?
    Check out the Tie::File module.

    --
    Gunnar Hjalmarsson
    Email: http://www.gunnar.cc/cgi-bin/contact.pl
  • Sanket vaidya at Mar 27, 2008 at 8:33 am
    Here is the entire code to accomplish your task. It will delete 1st & 3rd
    lines.

    use warnings;
    use strict;

    my @array;
    open FH,"data.txt";
    @array = <FH>;
    for my $i (0..$#array)
    {
    $array[$i] =~ s/^(\*\/tmp\/dst\/file(1|3)\*(\d){3}\*RW\*(\d){3,4})$/ /;
    #replace the lines you want to delete with " " (space)
    }
    my @result = grep(/[^\s]/,@array); #Take other lines in @result.
    close FH;

    open FH,">data.txt";
    print FH @result;


    Hope it is useful.








    -----Original Message-----
    From: btna
    Sent: Wednesday, March 26, 2008 9:21 PM
    To: beginners@perl.org
    Subject: How to replace one line in a text file with Perl?

    Hi all,

    I have a file that looks like:

    */tmp/dst/file1*234*RW*6790
    */tmp/dst/file2*568*RW*908
    */tmp/dst/file3*345*RW*746
    */test/flm/file4*354*RW*987
    */test/flm/file5*643*RW*645


    I need to keep all of the lines with the exception of the one with
    file1 and file3 which have in common "/tmp/dst". I need to preserve
    the line /tmp/dst/file2 plus the others that start with */test

    Any ideas on how to do this with perl?

    Thanks,

    BTNA


    --
    To unsubscribe, e-mail: beginners-unsubscribe@perl.org
    For additional commands, e-mail: beginners-help@perl.org
    http://learn.perl.org/
  • John W. Krahn at Mar 27, 2008 at 9:02 am
    [ Please do not top-post your replies. TIA ]


    sanket vaidya wrote:
    Here is the entire code to accomplish your task. It will delete 1st & 3rd
    lines.

    use warnings;
    use strict;

    my @array;
    open FH,"data.txt";
    You should *always* verify that the file opened correctly:

    open FH, '<', 'data.txt' or die "Cannot open 'data.txt' $!";

    @array = <FH>;
    my @array = <FH>;

    for my $i (0..$#array)
    {
    $array[$i] =~ s/^(\*\/tmp\/dst\/file(1|3)\*(\d){3}\*RW\*(\d){3,4})$/ /;
    If you have a lot of / characters in the pattern you should probably use
    a delimiter other than /. A character class is better for single
    character alternatives then using alternation and you don't use the
    captured strings so you don't need capturing parentheses.

    $array[$i] =~ s!^\*/tmp/dst/file[13]\*\d{3}\*RW\*\d{3,4}$! !;

    #replace the lines you want to delete with " " (space)
    }
    my @result = grep(/[^\s]/,@array); #Take other lines in @result.
    [^\s] could be written more simply as \S.

    Or you could just remove the elements from the original array:

    for my $i ( reverse 0 .. $#array ) {
    splice @array, $i, 1
    if $array[$i] =~ m!^\*/tmp/dst/file[13]\*\d{3}\*RW\*\d{3,4}$!;
    }

    close FH;

    open FH,">data.txt";
    You should *always* verify that the file opened correctly:

    open FH, '>', 'data.txt' or die "Cannot open 'data.txt' $!";

    print FH @result;

    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
  • Sanket vaidya at Mar 27, 2008 at 8:41 am
    Here is the entire code to accomplish your task. It will delete 1st & 3rd
    lines.

    use warnings;
    use strict;

    my @array;
    open FH,"data.txt";
    @array = <FH>;
    for my $i (0..$#array)
    {


    $array[$i] =~ s/^(\*\/tmp\/dst\/file(1|3)\*(\d){3}\*RW\*(\d){3,4})$/ /;
    #replace the lines you want to delete with " " i.e. (space)

    }

    my @result = grep(/[^\s]/,@array); #Take other lines in @result.
    close FH;

    open FH,">data.txt";
    print FH @result;


    Hope it is useful.








    -----Original Message-----
    From: btna
    Sent: Wednesday, March 26, 2008 9:21 PM
    To: beginners@perl.org
    Subject: How to replace one line in a text file with Perl?

    Hi all,

    I have a file that looks like:

    */tmp/dst/file1*234*RW*6790
    */tmp/dst/file2*568*RW*908
    */tmp/dst/file3*345*RW*746
    */test/flm/file4*354*RW*987
    */test/flm/file5*643*RW*645


    I need to keep all of the lines with the exception of the one with
    file1 and file3 which have in common "/tmp/dst". I need to preserve
    the line /tmp/dst/file2 plus the others that start with */test

    Any ideas on how to do this with perl?

    Thanks,

    BTNA


    --
    To unsubscribe, e-mail: beginners-unsubscribe@perl.org
    For additional commands, e-mail: beginners-help@perl.org
    http://learn.perl.org/

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupbeginners @
categoriesperl
postedMar 26, '08 at 3:52p
activeMar 27, '08 at 9:02a
posts5
users4
websiteperl.org

People

Translate

site design / logo © 2022 Grokbase