Re: Slightly OT: How to grep for two different things in a file
- Go to: [ bottom of page ] [ top of archives ] [ this month ]
Date: Thu, 08 Sep 2022 08:20:35 UTC
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