[97603] in RedHat Linux List

home help back first fref pref prev next nref lref last post

Re: grep syntax

daemon@ATHENA.MIT.EDU (William Stearns)
Wed Nov 4 00:08:18 1998

Date: Wed, 4 Nov 1998 00:11:11 -0500 (EST)
From: William Stearns <wstearns@pobox.com>
To: "Canary, Robert W. -- Robert W. Canary" <rwcanary@ohiocounty.net>,
  rwcanary@mail.lig.bellsouth.net
cc: ML-redhat <redhat-list@redhat.com>
In-Reply-To: <363FDCCB.265E@ohiocounty.net>
Resent-From: redhat-list@redhat.com
Reply-To: redhat-list@redhat.com

Good evening, Robert,

On Tue, 3 Nov 1998, Robert W. Canary wrote:

> I am trying find the correct syntax for grep a directory and excluding
> certain files from the output.
> 
> EXCEPTIONS="ttyC4 ttyC5"
> 
> command ls /var/lock | grep tty[SC] | {filter out $EXCEPTIONS}
> 
> I can filter out the exception with "grep -v ttyC4 | grep -v ttyC0", but
> I need to be able to feed it a variable that contians a list of some
> sort.  

	How about:

EXCEPTIONS="ttyC4|ttyC5"
ls /var/lock | grep tty[SC] | egrep -v "(${EXCEPTIONS})"

	The egrep (xxx|yyy|zzz) syntax tells grep to match anything with
xxx OR yyy OR zzz in it.  The -v says to discard anything with xxx or yyy
or zzz in it.  You do have to use the vertical pipe sysmbol as the
separator in your list.  If this is a problem, try:

EXCEPTIONS="ttyC4 ttyC5"  #Set by a previous part of your script, perhaps

EXCEPTIONS=`echo ${EXCEPTIONS} | sed -e 's/ /|/'`
ls /var/lock | grep tty[SC] | egrep -v "(${EXCEPTIONS})"

	Cheers,
	- Bill

---------------------------------------------------------------------------
Unix _is_ user friendly.  It's just very selective about who its friends 
are.  And sometimes even best friends have fights.
William Stearns (wstearns@pobox.com)
Mason, buildkernel, and named2hosts are at: http://www.pobox.com/~wstearns
---------------------------------------------------------------------------


-- 
  PLEASE read the Red Hat FAQ, Tips, Errata and the MAILING LIST ARCHIVES!
		http://www.redhat.com http://archive.redhat.com
         To unsubscribe: mail redhat-list-request@redhat.com with 
                       "unsubscribe" as the Subject.


home help back first fref pref prev next nref lref last post