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

Hello Monks,

I always find myself wanting to use an equivalent to grep on windows and seek a perl solution. I tracked down a script on the net and gave it some polishing up. It seems to behave.

#!/usr/bin/env perl use strict; use warnings; use Cwd; use File::Find; =pod =head1 DESCRIPTION Usage: perl grep1.pl frobnitz txt =cut my $search_pattern=$ARGV[0]; my $file_pattern =$ARGV[1]; find(\&d, cwd); sub d { my $file = $File::Find::name; $file =~ s,/,\\,g; return unless -f $file; return unless $file =~ /$file_pattern/; open my $fh, $file or print "couldn't open $file\n" && return; while (<$fh>) { if (my ($found) = m/($search_pattern)/o) { print "found $found in $file\n"; last; } } close $fh; }

My question is what this line is doing:

$file =~ s,/,\\,g;

Also accepting any criticisms/improvements.

Replies are listed 'Best First'.
Re: grep for windows
by RonW (Parson) on Oct 15, 2016 at 02:41 UTC
    My question is what this line is doing:
    $file =~ s,/,\\,g;

    It is replacing / characters with \ characters in $file. The g causes the replacement to apply to all occurrences.

    Commas were used as the delimiters to avoid escaping the / character. An equivalent statement would be:

    $file =~ s/\//\\/g;

    Which many people find harder to read.

Re: grep for windows
by Athanasius (Cardinal) on Oct 15, 2016 at 03:05 UTC

    Hello Datz_cozee75,

    Fleshing out RonW’s answer a little:

    My question is what this line is doing:

    $file =~ s,/,\\,g;

    It’s replacing / (forward slash) with \\ (backslash) throughout the string $file.

    Here, s introduces the substitution operator. It’s normally written s///, but since the string to be substituted is itself a forward slash, a different delimiter has been chosen: a , (comma). See perlretut#Simple-word-matching.

    The backslash is doubled because a single backslash introduces an escape sequence — see perlop#Quote-and-Quote-like-Operators. So two consecutive backslashes represent an actual (single!) backslash character.

    And of course the /g modifier on the regex causes the substitution to be made globally throughout the string. See perlre#Modifiers.

    Hope that helps,

    Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,

Re: grep for windows
by BrowserUk (Patriarch) on Oct 15, 2016 at 00:53 UTC

    Have you ever typed: findstr /? at a windows command line prompt? If not, try it.


    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority". I knew I was on the right track :)
    In the absence of evidence, opinion is indistinguishable from prejudice.

      Thx all for responses...they covered the gamut pretty well. I was unaware of this native use of msdos, and there is something to be said for going at a problem in its native environment. The recursive form was what I was looking for:

      C:\Users\Fred\Desktop>findstr /s Neskowin *.txt handyman\template_stuff\captions\a.txt:I'm dragging my tools out again +, getting them sharpened, adjusted for altitude. I can't help but not +ice that your Neskowin property needs a lot of the type of TLC I arri +ve with, beginning with oil for hinges.

      Examples here: https://www.windows-commandline.com/findstr-command-examples-regular/

      Cheers,

Re: grep for windows
by VinsWorldcom (Prior) on Oct 15, 2016 at 00:55 UTC
      on the same side you can also try unxutils

      L*

      There are no rules, there are no thumbs..
      Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.
Re: grep for windows
by clueless newbie (Curate) on Oct 15, 2016 at 00:53 UTC
    Since you were looking for something equivalent to grep, perhaps you'd be interested in ack.

      Few weeks ago I could not install ack 2, for the same purpose as OP, due to problem with Find::Next. Other details are there.

        Few weeks ago I could not install ack 2, for the same purpose as OP, due to problem with Find::Next. Other details are there.

        When a test fails because it couldn't create a temp file in a non existent temp directory ... force install it

Re: grep for windows
by NetWallah (Canon) on Oct 15, 2016 at 18:08 UTC
    Just adding a point of interest - RonW and Athanasius explain how to avoid the Leaning toothpick syndrome, which is also mentioned in perlop:

    ... you can use any pair of non-whitespace (ASCII) characters as delimiters. This is particularly useful for matching path names that contain "/" , to avoid LTS (leaning toothpick syndrome).

            ...it is unhealthy to remain near things that are in the process of blowing up.     man page for WARP, by Larry Wall

Re: grep for windows (perlpowertools)
by Anonymous Monk on Oct 15, 2016 at 01:48 UTC