pass cmd line file glob to grep for readdir

Ask a Question related to PERL Miscellaneous, Design and Development.

  1. #1

    Default pass cmd line file glob to grep for readdir

    Hi all

    I want to process lots of files (too many for the shell to expand on
    one line) so I use readdir instead of glob. If I know the file glob
    to use such as file*.ext I can put this directly into the grep ...
    local @ARGV = grep /file.*\.ext/, readir CURDIR;
    My question is how can I take a file glob on the command line and
    transform it into an expression for grep. I suppose this also implies
    we must prevent the shell from expanding by quoting the expression
    such as myscript *file*.ext"

    Thanks.
    qanda Guest

  2. Similar Questions and Discussions

    1. File::Glob bug ?
      The following script: use File::Glob ':glob'; my @test = ( '{random string}', '\\{random string\\}' ); for (@test) {
    2. grep text by line....anyone pls?
      how to grep text by line? anyone can help to guide me.
    3. variable file glob into grep without glob()
      Hope I don't get shot for posting again, but I really need help with this ... I want to process lots (thousands) of files. I want to take a file...
    4. grep binary file detection
      Hi all, When I grep through my mbox files, the ones that I imported from Outlook Express return "binary file matches". I have run dos2unix to...
    5. file name of file that reference to type glob points to
      Hey all. This is probably a elementary question, but I can't seem to be able to find a builtin function or combination of functions that will let...
  3. #2

    Default Re: pass cmd line file glob to grep for readdir

    On 8 Sep 2003 22:57:15 -0700, qanda <fumail@freeuk.com> wrote:
    >
    > My question is how can I take a file glob on the command line and
    > transform it into an expression for grep. I suppose this also implies
    > we must prevent the shell from expanding by quoting the expression
    > such as myscript *file*.ext"
    Why not use perl's glob function (perldoc -f glob)?

    --
    Sam Holden

    Sam Holden Guest

  4. #3

    Default Re: pass cmd line file glob to grep for readdir

    [email]fumail@freeuk.com[/email] (qanda) wrote in news:62b4710f.0309082157.4c16ee32
    @posting.google.com:
    > Hi all
    >
    > I want to process lots of files (too many for the shell to expand on
    Have you considered using the xargs shell command?

    --
    Eric
    $_ = reverse sort $ /. r , qw p ekca lre uJ reh
    ts p , map $ _. $ " , qw e p h tona e and print
    Eric J. Roode Guest

  5. #4

    Default Re: pass cmd line file glob to grep for readdir

    > Why not use perl's glob function (perldoc -f glob)?

    Because it won't work, as perldoc says, it returns the filename
    expansions from the shell, the underlying problem is because the shell
    has a limit in the number of characters allowed in one line (it may be
    big, maybe 1MB) but with thousands of files, each with 30 or 40
    character filenames that soon runs out. I don't want the user to try
    using find and/or xargs, I just want to call myscript file*.ext or
    quoted as in myscript "file*.ext" if need be.

    Thanks.
    qanda Guest

  6. #5

    Default Re: pass cmd line file glob to grep for readdir

    Sam Holden <sholden@flexal.cs.usyd.edu.au> wrote:
    > On 8 Sep 2003 22:57:15 -0700, qanda <fumail@freeuk.com> wrote:
    >>
    >> My question is how can I take a file glob on the command line and
    >> transform it into an expression for grep. I suppose this also implies
    >> we must prevent the shell from expanding by quoting the expression
    >> such as myscript *file*.ext"
    >
    > Why not use perl's glob function (perldoc -f glob)?

    This is a good idea if you have a perl version >= 5.6.0.

    (otherwise the glob will fail because of the shell's limitations)


    --
    Tad McClellan SGML consulting
    [email]tadmc@augustmail.com[/email] Perl programming
    Fort Worth, Texas
    Tad McClellan Guest

  7. #6

    Default Re: pass cmd line file glob to grep for readdir

    Eric J. Roode <REMOVEsdnCAPS@comcast.net> wrote:
    > [email]fumail@freeuk.com[/email] (qanda) wrote in news:62b4710f.0309082157.4c16ee32
    > @posting.google.com:
    >
    >> Hi all
    >>
    >> I want to process lots of files (too many for the shell to expand on
    >
    > Have you considered using the xargs shell command?

    This is a good idea if you have perl version < 5.6.0.


    --
    Tad McClellan SGML consulting
    [email]tadmc@augustmail.com[/email] Perl programming
    Fort Worth, Texas
    Tad McClellan Guest

  8. #7

    Default Re: pass cmd line file glob to grep for readdir

    On 9 Sep 2003 05:53:29 -0700, qanda <fumail@freeuk.com> wrote:
    >> Why not use perl's glob function (perldoc -f glob)?
    >
    > Because it won't work, as perldoc says, it returns the filename
    > expansions from the shell, the underlying problem is because the shell
    > has a limit in the number of characters allowed in one line (it may be
    > big, maybe 1MB) but with thousands of files, each with 30 or 40
    > character filenames that soon runs out. I don't want the user to try
    > using find and/or xargs, I just want to call myscript file*.ext or
    > quoted as in myscript "file*.ext" if need be.
    Maybe you should use a perl from this century, since the text:

    Beginning with v5.6.0, this operator is implemented using the
    standard "File::Glob" extension. See File::Glob for details.

    in the documentation seems to disagree with your claim.

    It might also be worthwhile finding a non-broken shell.

    ; cd /dev
    ; echo * * * * * * * * * * * * * * * * * * * * * * * \
    * * * * * * * * * * * * * * * * * * * * * * * \
    * * * * * * * * * * * * * * * * * * * * * * * \
    * * * * * * * * * * * * * * * * * * * * * * * \
    | wc
    1 128984 783012
    ; perl -le 'print join " ", <* * * * * * * * * * * * * * * * * * * * * * *'\
    ' * * * * * * * * * * * * * * * * * * * * * * *'\
    ' * * * * * * * * * * * * * * * * * * * * * * *'\
    ' * * * * * * * * * * * * * * * * * * * * * * *>' | wc
    0 128984 783012
    ; /bin/echo * * * * * * * * * * * * * * * * * * * * * * * \
    * * * * * * * * * * * * * * * * * * * * * * * \
    * * * * * * * * * * * * * * * * * * * * * * * \
    * * * * * * * * * * * * * * * * * * * * * * * \
    | wc
    bash: /bin/echo: Argument list too long

    The limit is with the operating system, not the shell, so why should perl's
    glob have any more difficulty than the shell's?

    Have you actually tried it out? My 10 second test above seems to disagree
    with your "it won't work" statement.

    --
    Sam Holden

    Sam Holden Guest

  9. #8

    Default Re: pass cmd line file glob to grep for readdir

    qanda <fumail@freeuk.com> wrote:
    >> Why not use perl's glob function (perldoc -f glob)?
    >
    > Because it won't work, as perldoc says, it returns the filename
    > expansions from the shell,

    Only with old perls.

    What perl version do you have?


    --
    Tad McClellan SGML consulting
    [email]tadmc@augustmail.com[/email] Perl programming
    Fort Worth, Texas
    Tad McClellan Guest

  10. #9

    Default Re: pass cmd line file glob to grep for readdir

    Thanks guys and girls, I wasn't aware it had been improved in the new
    versions; I'm limited (by customer) to using 5.005.

    As for the argument limit, yes the underlying issue is with the OS, as
    I understand it the limit is set by ARG_MAX in limits.h.

    Would anyone be able to answer the (original) question for me,
    certainly if using a newer version of Perl glob would be fine; however
    I can't do that for this particular problem and the only overhead I
    want for the user is to possibly quote the file glob so we avoid find
    and/or xargs, etc.

    Thanks again.
    qanda Guest

  11. #10

    Default Re: pass cmd line file glob to grep for readdir

    Sorry for posting on top of my last reply, but I really do need help
    with this one. I will be forever (well quite some time at least) in
    your debt if you can help :)
    qanda Guest

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139