Operating System - HP-UX
1846436 Members
2447 Online
110256 Solutions
New Discussion

Re: grep -E (with regular expression) \b

 
Reggie Chang
Frequent Advisor

grep -E (with regular expression) \b

Hi there,

I'm trying to use grep -E (regular expression)
with \b (word boundary) in csh without success.
(grep -E $path '\b\/usr....\b')

Is there any way to use \b, \s etc with grep?

Thanks.

Reggie
12 REPLIES 12
curt larson_1
Honored Contributor

Re: grep -E (with regular expression) \b

what version of grep are you using, what /usr/bin/grep?

at 11.11 grep has the -w option that matches only words
Reggie Chang
Frequent Advisor

Re: grep -E (with regular expression) \b

Curt,

I'm using 10.20. I don't know what
version if grep it is, but it is located
at /usr/bin.

Thanks.

Reggie
Bill Hassell
Honored Contributor

Re: grep -E (with regular expression) \b

grep at 10.20 does not have the -w (word boundary) option. You'll have to upgrade to 11.11 with the latest patches to have that feature. \b is not a part of regular expressions so grep can't parse it.


Bill Hassell, sysadmin
Reggie Chang
Frequent Advisor

Re: grep -E (with regular expression) \b

Bill,

Thank you for the info.

I did not see -w in grep man page.
I'll find some other way since I'm
stuck with 10.20 at this moment.

Thanks.

Reggie
curt larson_1
Honored Contributor

Re: grep -E (with regular expression) \b

you could also use the blank or space character classes

grep -E '[:space:]...[:space:]'
or
grep -E '[:blank:]...[:blank:]'

not even the -w switch is going to work for you in this case because both : and / are non word characters

your going to have to use perl or write your own

function notInPath {
typeset field
print $2 |
awk -v field=$1 -F: '{
if (NR > 1) exit;
for ( i=1; i<=NF; i++ ) {
if ( field == $i ) exit 1;
}
exit 0;}'
return
}

notInPath /usr/bin $PATH
if [[ $? = 0 ]] ;then
print "not in path"
else
print "it is in the path"
fi
Reggie Chang
Frequent Advisor

Re: grep -E (with regular expression) \b

Curt,

Thanks a lot for the info.

Please forgive my dumb questions.
Is function notInPath in Perl?
Does shell script (C-shell or others)
support function?

Best regards,

Reggie
curt larson_1
Honored Contributor

Re: grep -E (with regular expression) \b

the syntax for the function i provide is written for the posix shell or the korn shell. A portion of it could be call an awk script.

still using posix shell syntax, but using perl instead of awk

print "$PATH" |
/opt/perl/bin/perl -F: -ane '
foreach $str (@F) {
if ( $str =~ m|^/usr/bin$| ) {
exit 1;
}};exit 0;'

if [[ $? = 0 ]] ;then
print "not in path"
else
print "it is in the path"
fi

the above example doesn't use a function
curt larson_1
Honored Contributor

Re: grep -E (with regular expression) \b

another easy way would be

print $PATH |
tr ":" "\012" |
grep -x /usr/bin >/dev/null 2>&1

if [[ $? = 0 ]] ;then
print "/usr/bin is in \$PATH"
else
print "/usr/bin is not in \$PATH"
fi

of if you like to switch the logic

num=$(print $PATH |
tr ":" "\012" |
grep -cx /usr/bin >/dev/null 2>&1)

if [[ $num = 0 ]] ;then
print "/usr/bin is not in \$PATH"
else
print "/usr/bin is in \$PATH"
fi


Mark Grant
Honored Contributor

Re: grep -E (with regular expression) \b

I might be being a bit dim here but doesn't this do what you want

echo $path|perl -ne "if(/\b\/usr...\b/){print}"
Never preceed any demonstration with anything more predictive than "watch this"
john korterman
Honored Contributor

Re: grep -E (with regular expression) \b

Hi,
if you have to use a regular expression you probably have to adapt it to your needs. The following is just a suggestion:
# grep -E "(^|[^a-zA-Z0-9])ordet([^a-zA-Z0-9]|$)" infile

which will try to look for the literal string
ordet
in infile. It does not take boundaries like e.g. special characters or tabs into account, but has already developed into something that gives unix a reputation as not easily understandable.....

regards,
John K.

it would be nice if you always got a second chance
curt larson_1
Honored Contributor

Re: grep -E (with regular expression) \b

Mr. Grant,

in this case the data being compared makes using word boundaries impossible. He wants to do this on his path variable, a colon delimited string of directory names. In this situation the word boundary is :/, which isn't matched by perl's \b. \W comes closer, but still won't match if what he is looking for is at the begining of the line. And \W\/usr\/bin\W/ will match both /usr/bin and /usr/bin/X11.

So, you end up doing something like
s/^/ /;tr /:/ /;if ( \W\/usr\/bin\W/ )
to get it to work that way.
Bill Hassell
Honored Contributor

Re: grep -E (with regular expression) \b

To answer two questions about 10.20: no, grep will never have the -w option, and no, you do not have Perl version 5 in 10.20, only Perl 4 which is fairly incompatile with Perl 5 coding. 10.20 has been a dead end since Dec 2001.


Bill Hassell, sysadmin