Skip site navigation (1)Skip section navigation (2)
Date:      8 Sep 2022 10:20:35 +0200
From:      "John Levine" <johnl@iecc.com>
To:        freebsd-questions@freebsd.org
Cc:        steve@sohara.org
Subject:   Re: Slightly OT: How to grep for two different things in a file
Message-ID:  <20220908082035.DD637492065F@ary.local>
In-Reply-To: <20220908073116.b95bf1b1495863241cc26248@sohara.org>

next in thread | previous in thread | raw e-mail | index | archive | help
It appears that Steve O'Hara-Smith <steve@sohara.org> said:
>On Wed, 07 Sep 2022 20:43:21 -0700
>Carl Johnson <carlj@peak.org> wrote:
>
>> I use grep -l to just return a list of files that contain one pattern,
>> and then grep -l for the second pattern on that list.  That can be done
>> in one line for your example as follows:
>> 
>>   grep -li /tmp `grep -liR tid src/java`
>
>	This fails in the presence of filenames with spaces, which is a
>pity because it is very elegant.

Something like this should do it. The -print0 and -0 deal with spaces in
the filenames, and the xargs handles more filenames than fit in a single
command.

$ find src/java -type f -exec grep -lq tid '{}' \; -print0 | 
  xargs -0 grep -l /tmp

-- 
Regards,
John Levine, johnl@taugh.com, Primary Perpetrator of "The Internet for Dummies",
Please consider the environment before reading this e-mail. https://jl.ly



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?20220908082035.DD637492065F>