Re: Delete line if REGEX == true
by davidrw (Prior) on Aug 07, 2005 at 02:18 UTC
|
note that $. is just a integer that is the current line number -- it's not the actual line .. depending on your context, $_ might be that.. What's the rest of your code? Some samples of some common situations:
foreach my $line ( @lines ){
next if $line =~ /,{5}/;
# do something
}
perl -ne ' do_some_print_statement_here unless /,{5}/' some_file.txt
my @lines = ...;
my @good_lines = grep( $_ !~ /,{5}/, @lines );
| [reply] [d/l] [select] |
|
Since Unix is much more than an environment to run perl under, I'll offer another solution which may make more sense if all you're using perl for is deleting lines based on a regex. No point in dragging in a good, general purpose tool over a good, specific purpose tool when that's the purpose you need it for. ;-)
sed -e '/,,,,,/d' < source.txt > destination.txt
There are a lot of good tools that come with unix. They can be worth the time to investigate - even if you don't use them, they can be invaluable guides in designing your own code to do something similar in perl. | [reply] [d/l] |
|
good point. And of course besides sed there's grep, which i think is even better here (read: teaching environment) because of the direct parallel to perl.
grep -v ,,,,, source.txt > destination.txt
| [reply] [d/l] [select] |
|
Re: Delete line if REGEX == true
by AReed (Pilgrim) on Aug 07, 2005 at 05:27 UTC
|
However, if you insist on using perl... ;-)
use strict;
use warnings;
while (<>) {
print unless /,,,,,/;
}
...will do it. Or the command-line equivalent:
perl -ne "print unless /,,,,,/" infile > outfile
Please note that I only tested this under Windows, so you may have to change double-quotes to single-quotes on a Unix system. | [reply] [d/l] [select] |
|
The key observation here is that you're making a copy of the file without those lines in it. There's no straightforward way to delete the lines directly from the file; instead you have to think about it slightly backwards: print the lines you want to keep, and just skip the ones you want to delete. When you're done, you can rename the new file onto the original, or you can use perl's -i switch to take care of that for you.
| [reply] [d/l] [select] |
|
There is too. Add an -i.bak and forget the redirection. See perlrun for -i.
C.
| [reply] [d/l] |
|
Re: Delete line if REGEX == true
by neniro (Priest) on Aug 07, 2005 at 09:35 UTC
|
Another way to directly remove unwanted lines is using Tie::File and greping those lines you want:
#!/usr/bin/perl
use strict;
use warnings;
use Tie::File;
my @text;
tie @text, 'Tie::File', 'test_filter.txt'
or die "Something went wrong: $!\n";
@text = grep { !/,,,,,/ } @text;
untie @text;
exit;
| [reply] [d/l] |
Re: Delete line if REGEX == true
by anonymized user 468275 (Curate) on Aug 08, 2005 at 14:28 UTC
|
If the idea is to make it terse enough to compete with sed or grep, this is the tersest I have so far...
print grep !/\,\,\,\,\,/, <>;
| [reply] [d/l] |
Re: Delete line if REGEX == true
by Anonymous Monk on Jul 08, 2008 at 12:46 UTC
|
Its very easy to do.
I just do in a find replace window that excepts regular expressions
if "brother" is the string your looking for heres an example.
Replace ^.*brother.*\n with nothing ( blank)
| [reply] |
|
that wont remove lines without the "brother string"
so...
1 2 3 brother 3 2 1
4 5 6 sister 7 8 9
will reduce to
brother
4 5 6 sister 7 8 9
which then becomes the problem as originally stated.
| [reply] |