1832872 Members
2468 Online
110048 Solutions
New Discussion

egrep doesn't work

 
SOLVED
Go to solution
Konrad Hegner
Frequent Advisor

egrep doesn't work

Hi everybody
This question is for real UNIX-cracks ;-)

Initial position:
script: /etc/profile.company-sysconf
called file from the script: /tools/data/domain
- directory /tools/ have drwxr-xr-x root root
- directory /data have drwxr-xr-x snml dba
- file 'domain' have -rwxr-xr-x snml dba

We have a shell-script with the command:
cat /tools/data/domain | egrep [a-z]

We use this command on 53 machines. It works fine on 52 machines. It doesn't work only by one (!!!) machine.
All machines are similar.
The script and the file 'domain' are both copys from a other machine (we tried it from different machines).
We think, it isn't the problem from the script, because if we make this 'cat ..| egrep' command from the command line it doesn't work too!

It works with the following 'settings':
root# cd /tools/data/
root# cat domain | egrep [a-z] --> fine

root# cd /
root# cat /tools/data/domain --> fine (without egrep)

other users$ cat /tools/data/domain | egrep [a-z] --> fine

It DOESN'T work only with root (!!):
root# cat /tools/data/domain | egrep [a-z] --> bad

We was checking also the environments, we think everything is the same.

Have somebody any ideas. It is very strange
Thank you very much - Konrad
6 REPLIES 6
Geoff Wild
Honored Contributor

Re: egrep doesn't work

Do a which egrep as user and as root - are they the same?

You might need to use explicit path:

/usr/bin/egrep

Rgds...Geoff
Proverbs 3:5,6 Trust in the Lord with all your heart and lean not on your own understanding; in all your ways acknowledge him, and he will make all your paths straight.
H.Merijn Brand (procura
Honored Contributor

Re: egrep doesn't work

1. Why use cat? I've mentioned this many times before: it's a plain waste of computer resources

# egrep [a-z] /tools/data/domain

works much more efficient

2. the fact that it does not run most likely finds its existance in the fact that a file exists with a one character name in the rang of a..z

You should quote th regular expression, because it is also recognized by the shell

So, .... how does

# egrep '[a-z]' /tools/data/domain

work for you?

Are the versions of (e)grep the same along the line of machines?

Enjoy, have FUN! H.Merijn
Enjoy, Have FUN! H.Merijn
Graham Cameron_1
Honored Contributor

Re: egrep doesn't work

Your root account is picking up egrep from a different path than other accounts.

Use 'type egrep' to find out where from.

BTW egrep is obsolescent.
You should use grep -E.

And, as per previous post, the use of 'cat' is superfluous

-- Graham
Computers make it easier to do a lot of things, but most of the things they make it easier to do don't need to be done.
Jdamian
Respected Contributor

Re: egrep doesn't work

I agree with procure.

you must enclose the pattern in single quotes. If not,

[a-z]

will be expanded to any file whose name matches this pattern. For example: a file whose name os 'a' exists in your current directory. The following command line:

$ egrep [a-z] /etc/profile

is execute as:

$ egrep a /etc/profile

This is because shell expands and substitutes file patterns.
If no filename matches the pattern, shell doesn't substitute the pattern.

Konrad Hegner
Frequent Advisor

Re: egrep doesn't work

Thanks for your answers.

- /usr/bin/egrep doesn't help.

- all egrep's are from /usr/bin/egrep (which or type command).

-# egrep '[a-z]' /.../domain works fine (also with the cat-command). I think, this is a very good workaround, or it is THE solution.

But why I have this problem only by one machine (from 53!)?


The tip to work without 'cat ... | egrep' is good, but for me it is easier to read the script if I use it in 'my way'. And for 'save' CPU time, it's definitiv not necessary. But thank you anyway - I'm a rookie and I have to learn a lot.
Bill Hassell
Honored Contributor
Solution

Re: egrep doesn't work

The reason that egrep [a-z] fails for root can be demonstrated with this command:

touch a
echo [a-z]
a

In other words, the [a-z] is a regular expression which, unless enclosed in single quotes or the brakets escvaped with backslashes: \[a-x\] then the shell expands the expression BEFORE egrep ever sees the characters on the command line. Try this:

echo ?

You'll see all the files in the current directory that are exactly 1 character names. Another way to see this is to take your failing command and put echo in fron of it:

echo egrep [a-z]

then protect the expression:

echo egrep '[a-z]'

Most people forget that the shell automatically looks for regular expressions and tries to perform a match.

In addition to the recommendation to not use cat into egrep, egrep itself is overkill since it has extra code to process extended regular expressions, and a character class such as [a-z] is no an extended regexp. Plain old grep will work fine with your example. An example of an extended regexp is:

egrep 'err|warn|fail' some_file

which finds all lines with any of the 3 strings. However, egrep isn't even necessary for the above task as multiple -e options will do the same thing:

grep -e err -e warn -e fail some_file


Bill Hassell, sysadmin