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:
choroba@still ~ $ echo group1 > a choroba@still ~ $ echo group2 > b choroba@still ~ $ echo group11 > c choroba@still ~ $ perl 1223194.pl a b c Moving a into group1 Moving b into group2 Moving c into group11 choroba@still ~ $ ls a b c ls: cannot access 'a': No such file or directory ls: cannot access 'b': No such file or directory ls: cannot access 'c': No such file or directory ([2]) choroba@still ~ $ find group* -type f group1/a group11/c group2/b
($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,

In reply to Re: find strings in multiple files, then move files to folders that match string name by choroba
in thread find strings in multiple files, then move files to folders that match string name by jeff92802

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.