1753830 Members
8878 Online
108806 Solutions
New Discussion юеВ

Re: Grepping issues..

 
Patrick Ware_1
Super Advisor

Grepping issues..

Hello,

I found another problem with my disk-adding script today. When looking for disks, I use grep.
When I grep for the following disk sizes:

5242880

I also pick up these as well:
524288000

How do I specifically pick out one or the other, using grep, without resorting to the -v option?

If I can't use grep, I'll have to totally revamp the script...
8 REPLIES 8
James R. Ferguson
Acclaimed Contributor

Re: Grepping issues..

Hi Patrick:

You need to anchor your regular expression or modify it to fit the context you want to match.

For example:

# grep " 5242880 "

...would find matches that are bounded by a space on either side.

# grep "^5242880$"

...would demand that the string matched only if it was anchored to the beginning (^) and the end ($) of the line.

Regards!

...JRF...
Patrick Ware_1
Super Advisor

Re: Grepping issues..

Thanks for your reply. One more question:
I set the disk sizes to a variable called $SIZE. How would it work with a variable?
James R. Ferguson
Acclaimed Contributor

Re: Grepping issues..

Hi (again) Patrick:

# VAR=5242880
# echo " 5242880 "|grep " ${VAR} "
5242880

That is, reference the variable just like you would when you interpolate it the shell. Notice that is is good practive to encapsulate the variable name in curly braces as shown.

Regards!

...JRF...
Patrick Ware_1
Super Advisor

Re: Grepping issues..

I am not understanding.
Here is an example of the what I am doing:

http://www.unix.com/showthread.php?p=302171123&posted=1#post302171123

Patrick Ware_1
Super Advisor

Re: Grepping issues..

grep -w worked like a charm.

Thanks to all!

I am always open to better solutions.
James R. Ferguson
Acclaimed Contributor

Re: Grepping issues..

Hi (again):

Patrick, why is this post a VERBATIM copy from another forum site? INCLUDING someone else's suggestion to use the '-w' switch of grep !!!

...JRF...

Patrick Ware_1
Super Advisor

Re: Grepping issues..

I asked the same question over there. I always go back and post what worked for me.
Dennis Handly
Acclaimed Contributor

Re: Grepping issues..

>grep -w worked like a charm.
>I am always open to better solutions.

That's what I would have suggested.