[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Passing multiple search directories to grep
From: |
Andreas Kusalananda Kähäri |
Subject: |
Re: Passing multiple search directories to grep |
Date: |
Tue, 3 Aug 2021 15:51:10 +0200 |
On Tue, Aug 03, 2021 at 08:55:23AM -0400, Greg Wooledge wrote:
> On Tue, Aug 03, 2021 at 12:10:21PM +0000, hancooper wrote:
> > On Tuesday, August 3, 2021 11:32 AM, Greg Wooledge <greg@wooledge.org>
> > wrote:
> > > So, if this command works on your system:
> > >
> > > grep -rl PATH /tmp /var/tmp
> > >
> > > then the same command generated using an array expansion will also work.
>
> > Do you understand how grep distinguishes the search pattern from the search
> > directories ?
>
> Yes. The search pattern is the first argument string (after options
> have been processed and removed). All of the argument strings after
> the pattern are files or directories to be read. If there are no
> arguments after the pattern, then standard input is read.
>
> This is made clear by the man page:
>
> SYNOPSIS
> grep [-E|-F] [-c|-l|-q] [-insvx] -e pattern_list
> [-e pattern_list]... [-f pattern_file]... [file...]
>
> grep [-E|-F] [-c|-l|-q] [-insvx] [-e pattern_list]...
> -f pattern_file [-f pattern_file]... [file...]
>
> grep [-E|-F] [-c|-l|-q] [-insvx] pattern_list [file...]
>
> In the absence of -e and -f, you're using the last form, where the
> pattern_list is a single argument, followed by zero or more files.
>
> (The processing of directories with a -r option is a GNU extension.)
>
> > I would prefer that my script does not impose any restriction on the search
> > patterns allowed
> > by grep, because currently "$ptrn" is just a user-defined string.
>
> In that case, use the -- indicator before the pattern.
>
> grep -rl -- "$ptrn" "${dirlist[@]}"
>
> This will ensure that grep doesn't treat the pattern as an option, even
> if it happens to begin with a hyphen.
Alternatively, and IMHO better in a "showing intention" sort of way,
grep -r -l -e "$ptrn" -- "${dirlist[@]}"
I.e., use "-e" to designate the pattern, and then "--" to delimit the
file operands from the options. But depending on what the pathnames
that are outputted from this are used for, I might suggest using "find"
in combination with "grep -q" instead, without either of "-r" or "-l".
--
Andreas (Kusalananda) Kähäri
SciLifeLab, NBIS, ICM
Uppsala University, Sweden
.
- Passing multiple search directories to grep, hancooper, 2021/08/03
- Re: Passing multiple search directories to grep, Greg Wooledge, 2021/08/03
- Passing multiple search directories to grep, hancooper, 2021/08/03
- Re: Passing multiple search directories to grep, Greg Wooledge, 2021/08/03
- Re: Passing multiple search directories to grep,
Andreas Kusalananda Kähäri <=
- Passing multiple search directories to grep, hancooper, 2021/08/03
- Re: Passing multiple search directories to grep, Alex fxmbsw7 Ratchev, 2021/08/03
- Re: Passing multiple search directories to grep, Andreas Kusalananda Kähäri, 2021/08/03
- Re: Passing multiple search directories to grep, Greg Wooledge, 2021/08/03
- Passing multiple search directories to grep, hancooper, 2021/08/03