Help from any UNIX guru please. ( Update - problem solved )

gittyup

Diamond Member
Nov 7, 2000
5,036
0
0
Here is the problem.

I am doing a grep on a file. I only want lines where the search string is matched exactly.

command % grep exam filename

filename contents:
line 1)here:is:my:exam:contents
line 2)here:is:your:problem:example

The problem is that grep will return both lines because it matches all or part of the search string. Lines that conatina both "exam" and "example" are returned. I only want line 1 to be returned, an exact match. I have looked thru the man pages and I don't see any options that will work. I know it can be done. I am also looking into awk. Any ideas????
 

aimn

Banned
Feb 14, 2001
683
0
0
I dont know the answer but I know who does! Go to HP Forums and post this question. You will have an answer within minutes.


Actually I posted the question for you, so you wouldnt have to go through signing up and everything. Sounds like you might want to do that anyways. So to speed things up, I posted it quick. I will post there answer here.
 

aimn

Banned
Feb 14, 2001
683
0
0
If the word you are looking for is surrounded by a space, or any other character :) ; etc..) then you can do a: grep " exam " filename
 

KarlHungus

Senior member
Nov 16, 1999
638
0
0
If I get the problem correctly then sysadmin's suggestion should work.

grep "exam:" filename
 

gittyup

Diamond Member
Nov 7, 2000
5,036
0
0
Normally it would, but in this case, you never know what is going to be on either side of exam.

It may be :exam:
or :,exam:
or :exam,:
or :exam,,,:
or :;;exam,:

It varies quite a bit in format. That's why I need to just search for exam exactly how it is.:D
 

KarlHungus

Senior member
Nov 16, 1999
638
0
0
Try using egrep instead, and do the following

egrep "exam: |exam," filename

It should work for the examples you just gave.

Edit - uh oh, pipes made that string mad. There should not be a space after : in that expression.
 

aimn

Banned
Feb 14, 2001
683
0
0
There is an option to grep that will give you only the exact match of the string you enter.

Check the man page for grep.
The -x option returns only exact matches.
 

gittyup

Diamond Member
Nov 7, 2000
5,036
0
0
sysadmin

Yes, there is an option -x for fgrep. Although, I think the -x option will return if the pattern matches the entire line.

At any rate, I went ahead and coded a "C" solution to the problem. Thanks for all your suggestions people.
 

MereMortal

Golden Member
Oct 16, 2000
1,919
2
81
Did you try this?
% grep -w exam filename

Edit: This flag works, but unfortunately it is not standard. It seems to be on Sun OS's but not on IRIX.