When searching several different patterns, create a regex that matches all of them and search for it, so you only have to do one match per line. Sorting the string by length from the longest to the shortest makes "group11" not match "group1".
$ARGV is a special variable which contains the name of the currently open file when using the diamond operator. -d returns true when its parameter is a directory.
Run the script as
perl scriptname file1 file2 file3...
If the search terms contain special characters, everything can break: they might have a special meaning in the regex (so you'll need quotemeta, but you'll lose the direct correspondence to the directory names), but it also might be impossible to create a directory of such a name.
For the simple strings you mentioned, it works correctly, though:
#!/usr/bin/perl
use warnings;
use strict;
my @search = qw( group1 group2 group11 );
my $regex = join '|', sort { length $b <=> length $a } @search;
$regex = qr/($regex)/;
while (<>) {
if (my ($found) = /$regex/) {
print STDERR "Moving $ARGV into $found\n";
-d $found or mkdir $found or die $!;
rename $ARGV, "$found/$ARGV" or die $!;
}
}
Test run:
($q=q:Sq=~/;[c](.)(.)/;chr(-||-|5+lengthSq)`"S|oS2"`map{chr |+ord
}map{substrSq`S_+|`|}3E|-|`7**2-3:)=~y+S|`+$1,++print+eval$q,q,a,
|