[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: bash, grep, and regular expressions



Peter Simpson wrote:
On Friday 18 Feb 2005 16:38, Freddy Freeloader wrote:

Matt Zagrabelny wrote:

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.

the last sentence above is a little misleading. ls does that by default.

so examples of what you want are:

.bashrc
something.cc
bin/

examples of what you dont want are:

.ssh/
.gnome2/

is this correct?

-matt zagrabelny

What I'm trying to do is return something like this

test.txt
bin/

rather than

.ssh/
test.txt
bin/
.bashrc

I have found some regular expressions that will filter out specific
files and extensions, but not something that will filter exclusively on
the . that signfies a hidden file or directory.  The best luck I've had
in filtering is to do something like this:

ls -al | grep -e ^d | grep -e '[.][a-z]'

This will filter to return directories only with the first grep command
and then the second grep will return only hidden directories that begin
with small caps.  However, where I run into problems is:

ls -al | grep -e ^d | grep -e '[^.][a-z]'

You would think this would return only directories that begin with
anything except . and begin with lower case letters. This however is not
true.  It returns all directories and ignores case altogether.


Your search matches any string in the line that contains something that is not a period, followed by a lower case letter.

This matches pretty much anything.

What you want to do is limit this somewhat!

A better idea would be to match two numbers followed by a space, followed by your letter.

What works for me is:

ls -al | grep -e ^d | grep -e ':[0-9][0-9] [^\.][a-zA-Z]'

HTH.



Your expression returns an empty set.

You were correct in saying that my second expression was not selective enough. I had left a portion of it out.

ls -al | grep -e ^d | grep -e '\<[^.][a-z]'

This also returns all directories. It doesn't matter whether they are hidden or not. The \< should limit the expression to searching for the beginning of a word that starts with anything except a ".". However, it does not do that. It returns exactly the same thing as the other less selective statement. When I remove the ^ from [^.] it returns an empty set.

It's as if grep doesn't recognize the space between the time column and the file name column as a separator between words, but as just another character in a word.



Reply to: