in reply to find strings in multiple files, then move files to folders that match string name
$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:
Test run:#!/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 $!; } }
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,
|
|---|