Re: bash, grep, and regular expressions
Justin Guerin wrote:
On Thursday 17 February 2005 17:08, Freddy Freeloader wrote:
I'm pretty much a noob at regular expressions but have been studying
them lately using Jeffrey Friedl's book Mastering Regular Expressions
2nd ed.. I've run across something that has me puzzled. Here's what it
Now before you flame me this is done strictly as an exercise in regular
I have ls aliased to ls -al. What I've been attempting to do with grep
and regular expressions is list only non-hidden directories and/or
files. I am unable to come up with an expression that will elimate
hidden files and return non-hidden files at the same time.
ls -al | grep -v ' \.\<[a-zA-Z0-9].*\>' # returns everything
ls | grep -e '\<[^.][[:alnum:]]' # returns everything
ls | grep -e '\<[.][[:alnum:]]' # returns an empty set
I've tried many more expressions than these but none of them have worked
so far and the first one works on a Solaris 9 machine with a bash shell.
I also cannot write a back reference that uses parenthesis. I copied
examples right out of Friedl's book and they won't work either. I get a
"invalid back reference" error every time I try.
Anyone have any insight into this? I'm running sarge with a 2.6.9
kernel on one Debian box and a 2.6.10 kernel on another, and all
packages are the very latest packages available in sarge on both boxes.
How about ls -la | grep -v -w "\..*" ? Works here:
uname -a: Linux jguerin 2.6.6-1-686 #1 Wed May 12 14:57:57 EST 2004 i686
GNU bash, version 3.00.14(1)-release (i386-pc-linux-gnu)
Copyright (C) 2004 Free Software Foundation, Inc.
From the grep man page:
Select only those lines containing matches that form whole words. The test
is that the matching sub-string must either be at the beginning of
the line, or preceded by a non-word constituent character.
Similarly, it must be either at the end of the line or followed by a
non-word constituent character.
Word-constituent characters are letters, digits, and the underscore.
The problem with just using grep -v "\..*" is that the . can appear
anywhere. The problem with grep -v "^\..*" is that the . can only appear
at the beginning of a line.
Let me know if this works for you.
I see we are using different versions of bash. I'm using 2.05b.0(1)
-release as that's what comes with sarge.
Your expression returns all files and directories but one: "..".