Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer

Re^5: Search and delete lines based on string matching

by imp (Priest)
on Mar 13, 2007 at 14:57 UTC ( #604560=note: print w/replies, xml ) Need Help??

in reply to Re^4: Search and delete lines based on string matching
in thread Search and delete lines based on string matching

Ah, you specified that words had to be removed, not tokens that could be part of a word. For the token 'bin' which should be removed:
  1. foobin
  2. binary
  3. bin1
If all of those should be deleted then you can change that pattern from:
my $pattern = '\b(?:' . join('|', @tokens) . ')\b';
my $pattern = join('|', @tokens);
If you only want to match words that start with 'bin', and are followed only by non-alpha characters, then this:
my $pattern = '^(?:' . join('|', @tokens) . ')[^a-zA-Z]*$';
A revised copy that handles the deletion of tokens with a purely line based input:
#!/usr/local/bin/perl use strict; use warnings; if (@ARGV != 3) { print "Usage: $0 <pattern file> <input file> <output file>\n"; exit; } my ($pattern_filename, $source_filename, $dest_filename) = @ARGV; open my $pattern_fh, '<', $pattern_filename or die "Failed to open $pa +ttern_filename: $!"; my @tokens = (); while (my $line = <$pattern_fh>) { chomp $line; push @tokens, $line; } my $pattern = '^(?:' . join('|', @tokens) . ')[^a-zA-Z]*$'; print "Search pattern: $pattern\n"; open my $infile, "<", $source_filename or die "Failed to open $source +_filename: $!"; open my $outfile,">>", $dest_filename or die "Failed to open $dest_f +ilename: $!"; while(my $line = <$infile>) { print "input : $line"; if ($line =~ /$pattern/) { next; } print "output: $line"; print $outfile $line; } close($infile); close($outfile);

Replies are listed 'Best First'.
Re^6: Search and delete lines based on string matching
by brut (Initiate) on Mar 13, 2007 at 15:07 UTC
    Ok so this next code is not working in the sense it's not giving an error but its not producing the result.

    I will put my files as such I am using

    File A:

    A[0] B C
    File B:
    A[0] C
    File C should be:
    Also as in previous reply if instead of deleting you have to add contents of file A to end of file B with inserting some string before every string like "This string added from file A" and then the dtring from file A.

    Kindly reply

      For deletion you would use this pattern, sorry:
      while (my $line = <$pattern_fh>) { chomp $line; # Use quotemeta to allow for items like A[0] push @tokens, quotemeta($line); } my $pattern = '^(?:' . join('|', @tokens) . ')[^a-zA-Z]*$'; print "Search pattern: $pattern\n";

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://604560]
[Corion]: "Unmatched ) in regex; marked by <-- HERE in m/(&#9583;&# 9633;)&#9583;&# 65077; &#9531;&#9473;&# 9531;) <-- HERE / at -e line 1." :)
[Corion]: Meh - my "flip the table" string doesn't render here ;)
[choroba]: It renders correctly in pm-cb-g

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (8)
As of 2018-07-17 16:06 GMT
Find Nodes?
    Voting Booth?
    It has been suggested to rename Perl 6 in order to boost its marketing potential. Which name would you prefer?

    Results (372 votes). Check out past polls.