Advertisement
Guest User

remove common words from files

a guest
Sep 10th, 2013
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 1.40 KB | None | 0 0
  1. #!/usr:bin/perl
  2. use strict;
  3. use warnings;
  4.  
  5. my $target = $ARGV[0];
  6. my $delim = '_';
  7.  
  8. my @filenames = `ls $target`;
  9. my %common_words;
  10. my $first=1;
  11. for my $filename (@filenames)
  12. {
  13.         chomp($filename);                       # remove trailing newline if any
  14.         #$filename =~s/\.[^.]+$//;              # remove extention, beware with doubles like ".tar.gz"
  15.         my @words = split($delim, $filename);
  16.         print "words : @words\n";
  17.         if( $first )
  18.         {
  19.                 print "first name : $filename\n";
  20.                 foreach (@words)
  21.                 {
  22.                         $common_words{$_} = 1;
  23.                 }
  24.                 $first = 0;
  25.         }
  26.         else
  27.         {
  28.                 print "non-first name : $filename\n";
  29.                 foreach my $word (keys %common_words)
  30.                 {
  31.                         if (not grep(/$word/,@words))
  32.                         {
  33.                                 delete $common_words{$word};
  34.                         }
  35.                 }
  36.         }
  37. }
  38. foreach my $filename (@filenames)
  39. {
  40.         foreach my $word (keys %common_words)
  41.         {
  42.                 my $filename_new = $filename;
  43.                 $filename_new =~ s/$word$delim//g;
  44.                 $filename_new =~ s/$delim$word//g;
  45.                 system "mv $target$filename $target$filename_new";
  46.                 $filename = $filename_new;
  47.         }
  48. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement