in reply to Finding a string within a string

One line is all it takes. Here I suppress the lines that match foo by only printing the line if it does not. Redirect the output from STDOUT to a file out.txt and you are done. Use ' instead of " on *nix

C:\>type data.txt foo bar foo bar foo foo C:\>perl -n -e "m/foo/ or print" data.txt > out.txt C:\>type out.txt bar bar C:\>

To match lots of differect stuff you would just match m/this|that|other/ If you wantit long hand it might look like:

#!/usr/bin/perl -w use strict; $ARGV[0] or die "Useage ./$0 file_to_process > output.file\n"; my @finds = qw( this that other ); my $finds_re = join '|', map { quotemeta }@finds; $finds_re = qr/$finds_re/; open F, $ARGV[0] or die $!; while(<F>) { next if m/$finds_re/; print; } close F;

cheers

tachyon

s&&rsenoyhcatreve&&&s&n.+t&"$'$`$\"$\&"&ee&&y&srve&&d&&print

Replies are listed 'Best First'.
Re: Re: Finding a string within a string
by skyler (Beadle) on Apr 10, 2003 at 05:13 UTC
    Tachyon I appreciate your help. Here is my code. This code only suppresses a block, how could I modify this code to instead of suppressing a block it suppress a line. Here is the code:
    #!perl -w my ($fs, $cs, $rs, $ec, $ss); setdelimiters('|^~\&'); sub setdelimiters { ($fs, $cs, $rs, $ec, $ss) = map {quotemeta} split //, $_[0]; } use strict; my $buffer=""; my $suppress = 0; my @field; my %newblock; my %partofblock; my %skipaccount; $skipaccount{$_} = 1 for qw(77403 77404 77406 77407 77408 77409 77411 +77412 77413 77414 77416 77418); my $infile = 'c:/testfile.txt'; my $outfile = 'c:/idxfile.txt'; open IN, "<$infile" or die "Couldn't open $infile, $!"; open OUT,">$outfile" or die "Couldn't open $outfile, $!"; $newblock{$_} = 1 for qw(MSH); $partofblock{$_} = 1 for qw(EVN PID FT1); while (<IN>) { chomp(); if (/^(MSH)(.....)/) { setdelimiters($2); } @field = split /$fs/; if ($newblock{$field[0]}) { processbuffer(); } elsif (!$partofblock{$field[0]}) { print STDERR "Possible error: segment=$field[0] line $.\n"; } if ($field[0] eq "FT1") { $suppress = 1 if $skipaccount{$field[7]}; } $buffer .= "$_\n"; } processbuffer(); sub processbuffer { print OUT $buffer unless $suppress; $buffer=""; $suppress = 0; }
      perl -n -e "m/77403|77404|77406|77407|77408|77409|77411|77412|77413|77 +414|77416|77418/ or print" infile.txt > outfile.txt

      Otherwise you will need to be more exact ie specify what the data looks like and what your code is supposed to do. It is far from clear in what you asked and the code you posted :-)

      cheers

      tachyon

      s&&rsenoyhcatreve&&&s&n.+t&"$'$`$\"$\&"&ee&&y&srve&&d&&print

      Is there any logic to your indentation style, or do you just hit tab at random? :)

      Consistent indentation makes code much easier to read. If code is easy to read, you'll get more useful answers because more people will bother to try to understand.

      Juerd
      - http://juerd.nl/
      - spamcollector_perlmonks@juerd.nl (do not use).