1833873 Members
2149 Online
110063 Solutions
New Discussion

The grep pattern?

 
SOLVED
Go to solution
Jack_27
Advisor

The grep pattern?

Hi,there

I had a file test.dat which contained two lines as below:

MSC_ID
MSC_ID_LIST

Then I used "grep MSC_ID test.dat" just return
MSC_ID
MSC_ID_LIST

However I could not get the unique line that included "MSC_ID" only.
I tried :
grep "\" test.dat
egrep "\" test.dat
egrep '\' test.dat

All failed.How could I catch the "MSC_ID" line only?

Thanks
Jack

14 REPLIES 14
Hai Nguyen_1
Honored Contributor

Re: The grep pattern?

Jack,

Try this:

# grep -x MSC_ID test.dat

Hai
Jack_27
Advisor

Re: The grep pattern?

Hai,

I tried that but also failed.

$grep -x MSC_ID test.dat
$

Jack
Jack_27
Advisor

Re: The grep pattern?

Hai

Maybe I missed other infor,in fact it is:

MSC_ID 093-234
MSC_ID_LIST 1157

Ross Zubritski
Trusted Contributor

Re: The grep pattern?

the -w switch should do the trick.

Regards,

RZ
Tom Maloy
Respected Contributor

Re: The grep pattern?

If there is nothing else on the line, then you can use the "anchor" characters. A caret (^) anchors to the start of line, and a dollar sign ($) anchors to the end of line. So:

grep "^MSC_ID$" test.dat
Carpe diem!
Leif Halvarsson_2
Honored Contributor

Re: The grep pattern?

Hi,
another method is matching at the end of line

grep MSC_ID$ test.dat
Frank Slootweg
Honored Contributor

Re: The grep pattern?

Re:
MSC_ID 093-234
MSC_ID_LIST 1157

If you want the first, but not the second, then do

grep "MSC_ID " test.dat

i.e. a space after "MSC_ID". That will match "MSC_ID ", but not "MSC_ID_".
Pete Randall
Outstanding Contributor

Re: The grep pattern?

Seeing as how it's a two line file, you could always come at this from the other direction:

grep -v MSC_ID_LIST test.dat


Pete

Pete
Jack_27
Advisor

Re: The grep pattern?

Thank all people here.

And Frank,it does work as you mentioned,then what could be used when I really want to match a word precisely?
I checked the manual and they told me that the "\" would be fine.

Why the grep "\" has no result?

Cheers
Jack
Frank Slootweg
Honored Contributor

Re: The grep pattern?

> Why the grep "\" has no result?

Because it is not implemented in your version of HP-UX. I.e. "the manual" you read is probably not a HP-UX manual.

There are many threads on this subject. Please search for "grep -w" and you will probably find them.

Frank Slootweg
Honored Contributor
Solution

Re: The grep pattern?

HP-UX 11i, i.e. 11.11 or higher, has the "-w" (word) option, i.e.

> $ grep -w MSC_ID test.dat
> MSC_ID 093-234
> $

I am not aware of any *HP-UX* grep(1) versions which recognize "\".
Jean-Louis Phelix
Honored Contributor

Re: The grep pattern?

Hi,

I'm sorry but Ross already gave you the answer :^). To match an isolated word, use -w option of grep.

Regards.
It works for me (© Bill McNAMARA ...)
john korterman
Honored Contributor

Re: The grep pattern?

Hi Jack,
I think that grep -w is only available for 11.11 and newer.

You can make use of the underscore character that follows immediately after the string MSC_ID.
The underscore belongs to the :punct: class, and you can make an unreadable regular expression that says that if such a character??comes immediately after your string, then do not match it.
Infile example:

MSC_ID 093-234
MSC_ID_LIST 1157
MSC_ID.LIST 1157
MSC_ID;LIST 1157
MSC_ID:LIST 1157

Command on my 11.0 system:
# grep -E "MSC_ID[^[:punct:]]" ./infile

MSC_ID 093-234

See e.g. man tr for other classes,

regards,
John K.
it would be nice if you always got a second chance
James R. Ferguson
Acclaimed Contributor

Re: The grep pattern?

Hi Jack:

John K. is correct. The '-w' option for 'grep appears in HP-UX at 11.11. For earlier, standard HP-UX releases, we can embellish the matching to cover (more) cases, as for instance, where your token occurs anchored to the beginning or end of a line, etc. with:

# echo "MSC_ID"|grep -E -e "MSC_ID[^[:punct:]]" -e "MSC_ID$" | grep -E -e "[^[:punct:]]MSC_ID" -e "^MSC_ID"

Regards!

...JRF...