jeff92802 has asked for the wisdom of the Perl Monks concerning the following question:

Hi, This is my first question and I barely know anything with Perl. I have to imagine someone has done something like this before and maybe can get me going in the right direction...understand I stress I know almost nothing about perl. I need a perl script that will search the text inside of all syslog files in a folder. As soon as the search string is found, I want it to move the file into a folder that is named the same as the search string, then move on to the next file. I will have multiple search strings and multiple destination folders. Ex Search for "group1" and move it to group1 folder; search for "group2" and move it to group2 folder, etc. I will likely have a dozen or more strings and related folders. Search will be in thousands of files. Any help is appreciated as I am overwhelmed and way over my head looking at the search results I have found on the web. Thanks, Jeff
  • Comment on find strings in multiple files, then move files to folders that match string name

Replies are listed 'Best First'.
Re: find strings in multiple files, then move files to folders that match string name
by choroba (Cardinal) on Sep 27, 2018 at 23:33 UTC
    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,
Re: find strings in multiple files, then move files to folders that match string name
by GrandFather (Saint) on Sep 28, 2018 at 06:25 UTC

    You may be feeling underwhelmed by the extent and depth of the response you have had so far. It's not that we don't want to help, but that we want to see some effort from you first. We would much rather help sort out the most trivial or horrible code than just hand you an answer.

    Yes, we do understand how hard it is to know where to start with something like this. A way to get a handle on what you need to do is to work through a process by hand and take note of what you did. Then sketch out a program that follows those manual steps. By "sketch" I mean, write the program putting comments in for stuff you don't know how to do yet, but at least show loops and decision points so the flow of the code is sketched in.

    Once you have done that, post here again and we will help sort out the areas that are giving you grief.

    Optimising for fewest key strokes only makes sense transmitting to Pluto or beyond
Re: find strings in multiple files, then move files to folders that match string name
by AnomalousMonk (Archbishop) on Sep 28, 2018 at 00:43 UTC
    ... I know almost nothing about perl.

    perlintro might be a helpful read if you haven't studied it already. See also the Tutorials section of the Monastery.


    Give a man a fish:  <%-{-{-{-<