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

i have multiple text files with different names in a directory. I want to search for a string in all the text files and if the string found in any text file , i want to rename that text file to ABC.txt Can anyone help me in doing this perl script.

Replies are listed 'Best First'.
Re: Rename the text file WRT String
by NetWallah (Canon) on Jul 24, 2013 at 05:51 UTC
    Welcome to the Monastery!

    You will find monks a lot more helpful if you actually attempt to write some code, and have a specific issue you are struggling with.

    To get you started , please take a look ad the File::Find module to help you slect the files you want to process, and the perl rename function, or, preferably, the "move" function in the File::Copy module.

                 My goal ... to kill off the slow brain cells that are holding me back from synergizing my knowledge of vertically integrated mobile platforms in local cloud-based content management system datafication.

Re: Rename the text file WRT String
by davido (Cardinal) on Jul 24, 2013 at 05:48 UTC

    Which part are you having trouble with? Post what code you already have, and we can probably help you with the part that you're stuck on.


    Dave

Re: Rename the text file WRT String
by 2teez (Vicar) on Jul 24, 2013 at 06:54 UTC
Re: Rename the text file WRT String
by mtmcc (Hermit) on Jul 24, 2013 at 06:59 UTC
    If I understand what you mean, something like this might work:

    #! /usr/bin/perl use strict; use warnings; my $path = "./IN/"; my $string = 'ABC'; my @filesWithString; opendir (my $directory, $path) or die $!; while(readdir $directory) { next if $_ =~ m/^\./; my $fileName = "$path" . "$_"; open (my $checkForString, "<", "$fileName"); while (<$checkForString>) { print STDERR "$_"; if ($_ =~ m/$string/) { push (@filesWithString, $fileName); } } close $checkForString; } closedir $directory; for (@filesWithString) { my $newName = "$_".'.ABC.txt'; open (my $old, "<", $_); open (my $new, ">", $newName); while (<$old>) { print $new "$_"; } close $old; close $new; }

    Although there's probably a simpler way of doing it.
      (I applied my own code style and removed unnecessary braces, using $_ instead of explicit loop variables may have worked and may be considered more 'perlish', but I don't want to think about which of my loop stomped on which others' $_ so I'm using explicit variables. Handles for files and dirs have been replaced with a simple $fh/$dh. If these aren't sufficient then that's a good indication one has to many of them open at the same time. It also caught a bug where i was referring to $directory instead of $path.)
      #! /usr/bin/perl use strict; use warnings; # $ERRNO instead of $! amongst other stuff use English qw( -no_match_vars ); my $path = "./IN/"; my $string = 'ABC'; my @filesWithString; opendir my $dh, $path or die "Can not open $path: $ERRNO"; FILE: while (my $file = readdir $dh) { # ignore 'hidden' files and ., .. if ($file =~ m/^\./) { next FILE; } # opendir and this should be replace with File::Find # makes relocationg the perl script to a different directory # much easier my $filename = $path . $file; # always check return value for open, close open my $fh, '<', $filename or die "Can not open '$filename': $ERRNO"; # keep track of current line number for nice message my $linecount = 1; LINE: while (my $line = <$fh>) { # encase $string in \Q \E in case it contains regexp metachara +cters # (remove this if you actually want to use metachars) if ($line =~ m/\Q$string\E/) { print "Found string '$string' in file $filename (line $lin +ecount)\n"; push @filesWithString, $filename; # abort all further parsing - we already found the string last LINE; } $linecount++; } close $fh or die "Can not close filehandle for '$filename': $ERRNO"; } closedir $dh or die "Can not close $path: $ERRNO"; for my $file (@filesWithString) { my $newName = $file . '.ABC.txt'; print "Renaming $file to $newName\n"; # uncomment this after debugging / verification #rename $file, $newName; }
        Thank you for restyling my working code. I'm sure reddevil420 will find your styling helpful. By the way, as far as I'm aware, the behaviour of rename is system dependent, which is why I prefer not to use it.