LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   How do you grep a variable? (https://www.linuxquestions.org/questions/programming-9/how-do-you-grep-a-variable-794795/)

dbrazeau 03-11-2010 03:44 PM

How do you grep a variable?
 
It seems like this would be a common question but I could not find an answer?

I'm writing a script and I want to do a grep on a variable. In my case I'm performing a division and I want to grep the variable containing the results to see if it is a whole number.

Basically I want to do something like this:

grep "." $RESULT

acid_kewpie 03-11-2010 03:46 PM

by habit is to do something like "echo $VAR | grep whatever" which isn't the best way, but it's what falls out of my fingers each time. Does that cover what you want?

jlliagre 03-11-2010 03:49 PM

This should work:
Code:

echo "$RESULT" | grep "\."
Acid_kewpie was faster ...

colucix 03-11-2010 03:49 PM

Basically you may echo a variable and pipe the result to the grep command:
Code:

echo "$var" | grep pattern
Anyway, take in mind that the shell does not perform floating point arithmetic and you can check if the result is an integer or not, using the same tool that performed the division (bc, awk, perl, whatever...).

Edit: oops... too late! ;)

dbrazeau 03-11-2010 03:51 PM

Doesn't quite seem to work. Here is what I'm using to test it.

~#V=1.5; echo $V | grep "."
1.5

Which seem OK, but when I do this:
~#V=2; echo $V | grep "."
2

I would expect grep to not print anything on the second case.

dbrazeau 03-11-2010 03:53 PM

Ok it looks like I need a \ in from of my .

~ # V=2.5; echo $V | grep "\."
2.5

~ # V=2; echo $V | grep "\."
~ #


Thanks guys!

acid_kewpie 03-11-2010 03:53 PM

but you know your regex's right? "." means a single character, not a period. escape it - \. or use single quotes around it, not double.

jlliagre 03-11-2010 03:58 PM

Quote:

Originally Posted by dbrazeau (Post 3894792)
Doesn't quite seem to work. Here is what I'm using to test it.

~#V=1.5; echo $V | grep "."
1.5

Which seem OK, but when I do this:
~#V=2; echo $V | grep "."
2

Read closer my reply in post #3 ...

acid_kewpie 03-11-2010 04:06 PM

please keep up. At least I replied too late within a minute!

ghostdog74 03-11-2010 07:33 PM

don't need to use external tools, in bash

Code:

$ string="1.23"
$ case $string in  *"."* ) echo "$string";; esac


catkin 03-11-2010 08:31 PM

+1 for ghostdog74's suggestion of doing it in the shell without using grep. A more general test for an integer (as opposed to a decimal number with a fractional part) is
Code:

[[ "${RESULT//[0-9]/}" != '' ]] && { <whatever you want to do if RESULT is not integer>; }

ghostdog74 03-11-2010 08:42 PM

Quote:

Originally Posted by catkin (Post 3895100)
+1 for ghostdog74's suggestion of doing it in the shell without using grep. A more general test for an integer (as opposed to a decimal number with a fractional part) is
Code:

[[ "${RESULT//[0-9]/}" != '' ]] && { <whatever you want to do if RESULT is not integer>; }

another way

Code:

$ shopt -s extglob
$ case "123.1" in  +([0-9]) ) echo "integer";; *) echo "not integer";;esac
not integer
$ case "1231" in  +([0-9]) ) echo "integer";; *) echo "not integer";;esac
integer


jlliagre 03-11-2010 08:57 PM

Not as intuitive as previous answers but this should work in at least bash and ksh:
Code:

a=12.34
[[ -z ${a%%*.*} ]] && echo not integer || echo integer



All times are GMT -5. The time now is 03:42 AM.