[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: grep -
From: |
Bob Proulx |
Subject: |
Re: grep - |
Date: |
Mon, 11 Feb 2008 23:37:39 -0700 |
User-agent: |
Mutt/1.5.13 (2006-08-11) |
leondd1995 wrote:
> ypcat hosts | grep st[1-4]-nttd
> ...
> Now... if I run the command in any particular directory /, /root,
> /tmp/ etc... the commard properly provided a list of all host that
> met that st[1-4]-nttd - which is the four listed above.
>
> However, I unknowingly had an empty file named 'st1-nttd' (a file
> with the same name as one of the hosts I was greping for) while in
> /root. I ran ypcat hosts | grep st[1-4]-nttd, from /root and the
> output only showed 'st1-nttd'.
> ...
> Is this a bug in grep or am I simply using bad syntax?
You are not quoting your grep expression. Because [1-4] is a shell
file glob wildcard it will match any files that match it. You need to
keep it from matching filenames so that the string will be passed to
the command verbatim.
ypcat hosts | grep "st[1-4]-nttd"
In the bash man page:
Pathname Expansion
After word splitting, unless the -f option has been set, bash scans
each word for the characters *, ?, and [. If one of these characters
appears, then the word is regarded as a pattern, and replaced with an
alphabetically sorted list of file names matching the pattern. If no
matching file names are found, and the shell option nullglob is dis-
abled, the word is left unchanged. ...
Bob
- grep -, leondd1995, 2008/02/12
- Re: grep -, John Cowan, 2008/02/12
- Re: grep -,
Bob Proulx <=