in reply to Deleting a line out of text file

Your problem is that you cannot just edit a file in place. All you are changing is the line which is in a variable in memory, it never gets to the disk. The most concise way to do this is to open the file, and then print out all the lines except the one you don't want. Then you can redirect the output to another file. Also, some of perl's command-line arguments can help you accomplish that:

#!/usr/bin/perl -nw use strict print unless /$var/i; # or a perl one-liner perl -i.bak -nwe 'print unless /019/i' <file>

HTH

Addendum: RE: Abigail's comments: Added the -e switch. And the 'gi' is just following the orginal poster since 019 may not be the only data (s)he is looking for, didn't want the confuse the issue if they plugged in some other data and it was missing. I suppose the 'g' is superfluous in this case, guess you can safely remove that one.

Replies are listed 'Best First'.
Re: Deleting a line out of text file
by Abigail-II (Bishop) on Aug 13, 2003 at 15:36 UTC
    Why the /gi? Also, you need a -e switch.

    Abigail

Re: Re: Deleting a line out of text file
by Anonymous Monk on Aug 13, 2003 at 17:11 UTC
    Thanks to all. Here is the direction I wanted to go but this wipes out my whole file and just keeps the line I changed: Can someone tell me how I can fix this?
    my $fil = 'file.txt'; open(DATA, "$fil") || die "Can not open: $!\n"; my @dat = (<DATA>); close(DATA); open(DATA, ">$fil") || die "cant write: $!\n"; foreach (@dat) { if($_ =~ /number/) { s/number/NEWWORD/; print DATA $_; } } close(DATA);

      You need to print out all the other lines also.

      If you want to edit a line and leave it in the file then just get rid of the if.

      ... foreach (@dat) { s/number/NEWWORD/; print DATA $_; } close(DATA);

      This will change the lines that need changing and print everything into the file.

      There is another risk with this method that if the program dies before getting the file printed back out your original file which was erased on the second open will not have the complete data. This is what the backup options cover you against.

      This will do the translation and leaves a backup file in this case named file.txt.bak.

      perl -pi.bak -e 's/number/NEWWORD/;' file.txt

      The -p says to print every line, the -i.bak says make a backup file by adding .bak to the input file name, -e says to run the next argument as a perl script. The substitute only happens if the pattern matched so there is no need for an if unless you want to locate one pattern and then replace another pattern with yet a different replacement.

        Here's another way, using grep to exclude lines matching the unwanted pattern as they are read in from the file:
        ... use strict; my $file = 'data_file.txt'; my $bad_pattern = '019'; open my $in_fh, "<$file" or die "can't open $file to read"; my @good_lines = grep { !/$bad_pattern/ } <$in_fh>; close $in_fh; open my $out_fh, ">$file" or die "can't open $file to write"; print $out_fh $_ foreach @good_lines; close $out_fh;
        thanks it now works with what you gave me:
        foreach (@dat) { s/number/NEWWORD/; print DATA $_; } close(DATA);
        Not sure I can use the perl command line backup example you gave me:
        perl -pi.bak -e 's/number/NEWWORD/;' file.txt
        because this is in a cgi web page script. Can I do some backup work as you suggested with this?
        foreach (@dat) { s/number/NEWWORD/; print DATA $_; } close(DATA);
Re: Re: Deleting a line out of text file
by crouchingpenguin (Priest) on Aug 14, 2003 at 02:38 UTC

    ...you cannot just edit a file in place...

    This has been coming up a lot recently. See Tie::File:

    "Tie::File" represents a regular text file as a Perl array. Each element in the array corresponds to a record in the file. The first line of the file is element 0 of the array; the second line is element 1, and so on. The file is not loaded into memory, so this will work even for gigantic files. Changes to the array are reflected in the file immediately.

    For an example of changing a file in place using Tie::File, see this node and this node.


    cp
    ----
    "Never be afraid to try something new. Remember, amateurs built the ark. Professionals built the Titanic."