chomp text files and return regex matches by product


/ Published in: Perl
Save to your folder(s)



Copy this code and paste it in your HTML
  1. #!/usr/bin/perl
  2.  
  3. use strict;
  4. use warnings;
  5.  
  6. die "usage $0 <input> <output>\n" unless ($ARGV[0] && $ARGV[1]);
  7.  
  8. my $input = shift;
  9. my $output = shift;
  10.  
  11. open (INPUT, '<', $input) or die "$input could not be opened for reading$!\n";
  12. open (OUTPUT, '>', $output) or die "$output could not be opened for writing$!\n";
  13.  
  14. my %products;
  15. while (<INPUT>) {
  16. #initialize a hash of arrays keyed by product, with an array of all filenames for that product
  17. if (m/ (\w*?)_.*sql/) {
  18. my $product = $1;
  19. if (m/ (${product}_02.*?sql)$/) {
  20. $products{$product} = [] unless defined $products{$product};
  21. push ( @{ $products{$product} }, $1 );
  22. }
  23. }
  24. }
  25.  
  26. #print the largest element of the array for each product
  27. foreach (values %products) {
  28. local $\ = "\n";
  29. print OUTPUT (sort( @{$_ } ))[-1];
  30. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.