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

Re: bash, grep, and regular expressions



Mike Kuhar wrote:

|
|
|Hi All,
|
|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 is.
|
|Now before you flame me this is done strictly as an exercise |in regular |expressions.
|
|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.
|
|TIA
|
|Gary
|

Gary, I'm not sure if this is just playing around, or you're trying to
adress a real situation.  If it's the second, then you're doing two
opposing things.  First, you're calling ls with the -a switch which
specifically requests listing all hidden files and directories,
From the ls manpage:
-a will show you ALL the files in the directory, including hidden files

then your're trying to negate seeing those files and directories with
the grep command.  If you just do ls -l(lower case L), you'll get the
long listing without the hidden stuff.  If you do ls -1(number 1), then
you will only see one visible filename per line without size, owner,
date or permission info.

mike



I realize that I'm doing two opposing things with my usage of ls -al. My purpose for what I'm doing is to learn regular expressions as I'm wanting to increase my scripting skills. This is just something I ran across that I can't accomplish with regular expressions and it seems that, on my machines anyway, no one else has been able to do this either, though some have given me expressions that will accomplish this on their machines.

What I'm not understanding here is why grep will read the dot of a hidden file and filter its return set when the dot is at the start of line as in the output of ls -a, but not at the start of a word as in the output of ls -al. There should be no difference as far as grep and regular expressions are concerned. I can do this with any file, but just not the output of ls -al.

The second problem I have, but which no one has addressed, is why won't back references work? I get an "invalid back reference" whenever I stick the necessary parenthesis in the expression. I have used examples that other people say they have used and they fail every time with the same error. I remove the parenthesis and the error no longer appears, but then the expression returns an empty set every time too, even when I know I'm running it against a file that contains what the expression is looking for.

Now when I say I'm a noob, that means I'm haven't been working with Debian for years. I have been studying it 8 hours a day for the for several months, so I've probably got more knowledge of the system than a lot of people who have used it for a couple of years, but haven't been reading books, researching, and experimenting with it constantly as I have. I still have lots of areas that I don't know nearly as well as I would like, but I'm not completely clueless.



Reply to: