1753805 Members
7381 Online
108805 Solutions
New Discussion юеВ

matching exact string

 
SOLVED
Go to solution
lawrenzo_1
Super Advisor

matching exact string

Hello all,

I want to match an exact string in a search so:

how do I get a system to only report on a string I specify:

my file contains say multiple strings with 50 in but I only want to display 50 and not 500 or 150 etc

any help would be great

thanks

Chris
hello
8 REPLIES 8
James R. Ferguson
Acclaimed Contributor
Solution

Re: matching exact string

Hi Chris:

# echo "500 lines\nor 50 lines \nor 55000 lines "|perl -nle 'print if /\b50\b/'
or 50 lines

This matches '50' only at boundries like whitespace or non-word characters.

Regards!

...JRF...
A. Clay Stephenson
Acclaimed Contributor

Re: matching exact string

It would help if you specified your tool (grep, sed, awk, perl, ...) but here's the grep solution and the other RE's will be similar.

grep -e '^50 ' -e ' 50$' -e '^50$' -e ' 50 ' infile > outfile

This will find 50 at the beginning of a string followed by a space, 50 preceded by a space at the end of a string, a line containing only 50 and a 50 with spaces before and after.
If it ain't broke, I can fix that.
Sandman!
Honored Contributor

Re: matching exact string

If you want to search and print only the string 50 then you do something like:

# egrep "[^0-9][5][0][^0-9]|^[5][0]|[5][0]$" file
blah2blah
Frequent Advisor

Re: matching exact string

grep -e "[:space:]50[:space:]"
Dennis Handly
Acclaimed Contributor

Re: matching exact string

If you want to search for an exact string you should use "grep -w 50". (Available on 11i.)
James R. Ferguson
Acclaimed Contributor

Re: matching exact string

Hi (again):

> Dennis: If you want to search for an exact string you should use "grep -w 50".

Well, I would agree if/when you are using 'grep', but the tool choice was never stated. Hence, TMTOWTDI ;-)

Regards!

...JRF...

Dennis Handly
Acclaimed Contributor

Re: matching exact string

>JRF: I would agree if/when you are using 'grep'

My comment was more pointed at the 3 times as many replies using grep.
lawrenzo_1
Super Advisor

Re: matching exact string

great stuff, pleanty of examples to mess with there.

Thanks again all!

Chris.
hello