From nobody Thu Sep 8 03:43:21 2022 X-Original-To: freebsd-questions@mlmmj.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mlmmj.nyi.freebsd.org (Postfix) with ESMTP id 4MNQ2P2Mgyz4bxlt for ; Thu, 8 Sep 2022 03:43:25 +0000 (UTC) (envelope-from carlj@peak.org) Received: from mail.nrtc.syn-alias.com (mail.nrtc.syn-alias.com [129.213.214.220]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (Client did not present a certificate) by mx1.freebsd.org (Postfix) with ESMTPS id 4MNQ2N0GZ5z46br for ; Thu, 8 Sep 2022 03:43:23 +0000 (UTC) (envelope-from carlj@peak.org) X-Authed-Username: Y2FybGpAcGVhay5vcmc= Received: from [199.58.99.70] ([199.58.99.70:38919] helo=bay.localnet) by mail.peak.org (envelope-from ) (ecelerity 4.4.0.19839 r(msys-ecelerity:tags/4.4.0.0^0)) with ESMTPA id 5D/71-03956-AD469136; Wed, 07 Sep 2022 23:43:23 -0400 Received: from carlj by bay.localnet with local (Exim 4.95 (FreeBSD)) (envelope-from ) id 1oW8Rl-0007KO-52 for freebsd-questions@freebsd.org; Wed, 07 Sep 2022 20:43:21 -0700 From: Carl Johnson To: freebsd-questions@freebsd.org Subject: Re: Slightly OT: How to grep for two different things in a file References: Date: Wed, 07 Sep 2022 20:43:21 -0700 In-Reply-To: (Aryeh Friedman's message of "Wed, 7 Sep 2022 18:00:36 -0400") Message-ID: <86edwmmsyu.fsf@bay.localnet> User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/28.1 (berkeley-unix) List-Id: User questions List-Archive: https://lists.freebsd.org/archives/freebsd-questions List-Help: List-Post: List-Subscribe: List-Unsubscribe: Sender: owner-freebsd-questions@freebsd.org X-BeenThere: freebsd-questions@freebsd.org MIME-Version: 1.0 Content-Type: text/plain X-Vade-Verdict: clean X-Vade-Analysis-1: gggruggvucftvghtrhhoucdtuddrgedvfedrfedtuddgjeeiucetufdoteggodetrfdotffvucfrrhho X-Vade-Analysis-2: fhhilhgvmecuufgjpfetvefqtfdppfftvfevpdfgpfggqdfptffvvedpqfgfvfenuceurghilhhouhht X-Vade-Analysis-3: mecufedtudenucenucfjughrpefhvffufhffjgfkfgggtgesthdttddttdertdenucfhrhhomhepvegr X-Vade-Analysis-4: rhhlucflohhhnhhsohhnuceotggrrhhljhesphgvrghkrdhorhhgqeenucggtffrrghtthgvrhhnpedv X-Vade-Analysis-5: geffueejudffieekfeettdeftdelleeuhfehfefggeefiedukefgleejieehgeenucfkphepudelledr X-Vade-Analysis-6: heekrdelledrjedtnecuvehluhhsthgvrhfuihiivgeptdenucfrrghrrghmpehinhgvthepudelledr X-Vade-Analysis-7: heekrdelledrjedtpdhhvghlohepsggrhidrlhhotggrlhhnvghtpdhmrghilhhfrhhomheptggrrhhl X-Vade-Analysis-8: jhesphgvrghkrdhorhhgpdhrtghpthhtohepfhhrvggvsghsugdqqhhuvghsthhiohhnshesfhhrvggv X-Vade-Analysis-9: sghsugdrohhrghdpmhhtrghhohhsthepshhmthhptddvrdhnrhhttgdrvghmrghilhdqrghshhdurdhs X-Vade-Analysis-10: hihntgdrlhgrnhdpnhgspghrtghpthhtohepuddpihhspghnrgepthhruhgvpdgruhhthhgpuhhsvghr X-Vade-Analysis-11: pegtrghrlhhjsehpvggrkhdrohhrgh X-Vade-Client: NRTC X-Rspamd-Queue-Id: 4MNQ2N0GZ5z46br X-Spamd-Bar: --- Authentication-Results: mx1.freebsd.org; dkim=none; dmarc=pass (policy=none) header.from=peak.org; spf=pass (mx1.freebsd.org: domain of carlj@peak.org designates 129.213.214.220 as permitted sender) smtp.mailfrom=carlj@peak.org X-Spamd-Result: default: False [-3.80 / 15.00]; NEURAL_HAM_MEDIUM(-1.00)[-1.000]; NEURAL_HAM_LONG(-1.00)[-1.000]; NEURAL_HAM_SHORT(-1.00)[-1.000]; DMARC_POLICY_ALLOW(-0.50)[peak.org,none]; R_SPF_ALLOW(-0.20)[+ip4:129.213.214.220]; MIME_GOOD(-0.10)[text/plain]; RCVD_IN_DNSWL_NONE(0.00)[129.213.214.220:from]; RCVD_TLS_LAST(0.00)[]; FROM_EQ_ENVFROM(0.00)[]; R_DKIM_NA(0.00)[]; MLMMJ_DEST(0.00)[freebsd-questions@freebsd.org]; RCVD_VIA_SMTP_AUTH(0.00)[]; RCPT_COUNT_ONE(0.00)[1]; ASN(0.00)[asn:31898, ipnet:129.213.208.0/21, country:US]; ARC_NA(0.00)[]; RCVD_COUNT_THREE(0.00)[3]; FROM_HAS_DN(0.00)[]; TO_DN_NONE(0.00)[]; TO_MATCH_ENVRCPT_ALL(0.00)[]; MIME_TRACE(0.00)[0:+] X-ThisMailContainsUnwantedMimeParts: N Aryeh Friedman writes: > I have 2 patterns I need to find in a given set of files. A file only > matches if it contains *BOTH* patterns but not in any given > relationship as to where they are in the file. In the past I have > used piped greps when both patterns are on the same line but in my > current case they are almost certainly not on the same line. > > For example my two patterns are "tid" (String variable name) and > "/tmp" [String literal] (i.e. the full string is the concatenation of > the two patterns I would do: > > grep -Ri tid src/java|grep -i /tmp > > But since /tmp is in a symbolic constant defined elsewhere (in a > different Java file) I need to find programmatically either the name > of the constant (has different names in different classes) and then do > the piped grep above with it or I need to look for the two patterns > separately and say a file is only accepted if it has both. > > P.S. The reason for this is I am attempting to audit my code base to > see what classes leave behind orphaned temp files. 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` I hope that gives you some ideas. -- Carl Johnson carlj@peak.org