From owner-freebsd-questions@FreeBSD.ORG Sat Jun 30 20:24:08 2012 Return-Path: Delivered-To: freebsd-questions@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [69.147.83.52]) by hub.freebsd.org (Postfix) with ESMTP id ADB641065787 for ; Sat, 30 Jun 2012 20:24:08 +0000 (UTC) (envelope-from gkeramidas@gmail.com) Received: from mail-ee0-f54.google.com (mail-ee0-f54.google.com [74.125.83.54]) by mx1.freebsd.org (Postfix) with ESMTP id 3C1C28FC1A for ; Sat, 30 Jun 2012 20:24:08 +0000 (UTC) Received: by eeke49 with SMTP id e49so1925899eek.13 for ; Sat, 30 Jun 2012 13:24:00 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=sender:from:to:cc:subject:references:date:in-reply-to:message-id :user-agent:mime-version:content-type; bh=NhkMEB5hnPywNq/+qyqihR+VOb/tZt1C5VLAk2ZcL2E=; b=gZSlrC1ybiQCOU39g3KPSyfriwkkPD5pgfLGH6J81WzcxK4W2ltc3zxO4DPlngcQTG nO765zFL97hnBSIiDKD2Frp93mNvl3143Gk+strrKcH9OGKTvt2kEfhv/xjiE58b9IRE GY8M2WydGizA5Sc74CR5ZoYaXp6txz5WZnLK8xxqd0p/alvqNxLCl7G1VgYY0Yb+/Xkc Tt/6B8seMC1aHFQ0/05k0Tv7Fxh0bMu7wHzmTVCWPuYeK1+5ZizNoHLMs9UNn8qj8XZl CBfjsUudBlf/uQqgm0R9B+eOBtlnjPdarn4/fKbOWYZHe2+5F+pU5PUTsgnKf+SKVsMd NF0g== Received: by 10.14.95.6 with SMTP id o6mr1580626eef.144.1341087840158; Sat, 30 Jun 2012 13:24:00 -0700 (PDT) Received: from kobe.gmail.com (217-162-217-29.dynamic.hispeed.ch. [217.162.217.29]) by mx.google.com with ESMTPS id y54sm19367947eef.10.2012.06.30.13.23.59 (version=TLSv1/SSLv3 cipher=OTHER); Sat, 30 Jun 2012 13:23:59 -0700 (PDT) Sender: Giorgos Keramidas From: Giorgos Keramidas To: Aleksandr Miroslav References: Date: Sat, 30 Jun 2012 22:23:57 +0200 In-Reply-To: (Aleksandr Miroslav's message of "Wed, 27 Jun 2012 10:25:46 -0400") Message-ID: User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/24.1.50 (gnu/linux) MIME-Version: 1.0 Content-Type: text/plain Cc: freebsd-questions@freebsd.org Subject: Re: shell scripting: grepping multiple patterns, logically ANDed X-BeenThere: freebsd-questions@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: User questions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Sat, 30 Jun 2012 20:24:08 -0000 On Wed, 27 Jun 2012 10:25:46 -0400, Aleksandr Miroslav wrote: > hello, > > I'm not sure if this is the right forum for this question, but here > goes. > > I have the following in a shell script: > > #!/bin/sh > > if [ "$#" -eq "0" ]; then > find /foo > fi > if [ "$#" -eq "1" ]; then > find /foo | grep -i $1 > fi > if [ "$#" -eq "2" ]; then > find /foo | grep -i $1 | grep -i $2 > fi > if [ "$#" -eq "3" ]; then > find /foo | grep -i $1 | grep -i $2 | grep -i $3 > fi > > Is there an easier/shorter way to do this? If there are 15 arguments > supplied on the command line, I don't necessarily want to build 15 if > statements. The solutions proposed so far are ok, if you really *have* to stick to a shell script. For safer regexp pattern construction I'd probably convert the script to some language that makes it less easy to shoot yourself in the foot because you missed a quote or because you happened to choose "'" as the quoting character and one of the patterns includes it too. I'd write this sort of logic in python, constructing 'regexp' patterns on the go from the command-line arguments: #!/usr/bin/env python import sys import re if __name__ == '__main__': pattern = '^.*$' # Match everything by default matcher = None if len(sys.argv) > 1: pattern = ( r'^.*(' + '|'.join(map(lambda part: r'' + part, sys.argv[1:])) + ').*$') try: # print 'pattern = %s' % pattern matcher = re.compile(pattern) except: sys.stderr.write('invalid pattern: %s' % pattern) sys.exit(1) for line in sys.stdin.readlines(): # print '# line = "%s", match = %s' % ( # line.rstrip(), matcher.match(line)) if matcher.match(line): line = line.rstrip() print '%s' % line This should be able to match even patterns with quotes embedded, e.g.: 0630 22:21 kobe:~$ ./foo.py "it's dark" When it's dark, the world is normally a quieter place When it's dark ^D When it's dark, When it's dark 0630 22:22 kobe:~$