Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game. |
| Notices |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
 |
GNU/Linux Basic Guide
This 255-page guide will provide you with the keys to understand the philosophy of free software, teach you how to use and handle it, and give you the tools required to move easily in the world of GNU/Linux. Many users and administrators will be taking their first steps with this GNU/Linux Basic guide and it will show you how to approach and solve the problems you encounter.
Click Here to receive this Complete Guide absolutely free. |
|
 |
03-11-2010, 03:44 PM
|
#1
|
|
Member
Registered: Aug 2009
Distribution: DENX Embedded Linux, OpenSuse
Posts: 138
Rep:
|
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
|
|
|
|
03-11-2010, 03:46 PM
|
#2
|
|
Moderator
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 42,676
|
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?
|
|
|
|
03-11-2010, 03:49 PM
|
#3
|
|
Moderator
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris10, Solaris 11, Ubuntu, OL
Posts: 9,311
|
This should work:
Code:
echo "$RESULT" | grep "\."
Acid_kewpie was faster ...
Last edited by jlliagre; 03-11-2010 at 03:50 PM.
|
|
|
|
03-11-2010, 03:49 PM
|
#4
|
|
Moderator
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.4 OpenSuSE 12.2
Posts: 9,896
|
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! 
Last edited by colucix; 03-11-2010 at 03:50 PM.
|
|
|
|
03-11-2010, 03:51 PM
|
#5
|
|
Member
Registered: Aug 2009
Distribution: DENX Embedded Linux, OpenSuse
Posts: 138
Original Poster
Rep:
|
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.
|
|
|
|
03-11-2010, 03:53 PM
|
#6
|
|
Member
Registered: Aug 2009
Distribution: DENX Embedded Linux, OpenSuse
Posts: 138
Original Poster
Rep:
|
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!
|
|
|
|
03-11-2010, 03:53 PM
|
#7
|
|
Moderator
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 42,676
|
but you know your regex's right? "." means a single character, not a period. escape it - \. or use single quotes around it, not double.
|
|
|
|
03-11-2010, 03:58 PM
|
#8
|
|
Moderator
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris10, Solaris 11, Ubuntu, OL
Posts: 9,311
|
Quote:
Originally Posted by dbrazeau
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 ...
|
|
|
|
03-11-2010, 04:06 PM
|
#9
|
|
Moderator
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 42,676
|
please keep up. At least I replied too late within a minute!
|
|
|
|
03-11-2010, 07:33 PM
|
#10
|
|
Senior Member
Registered: Aug 2006
Posts: 2,695
|
don't need to use external tools, in bash
Code:
$ string="1.23"
$ case $string in *"."* ) echo "$string";; esac
|
|
|
|
03-11-2010, 08:31 PM
|
#11
|
|
LQ 5k Club
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian Squeeze (server), Slackware 13.37 (netbook), Slackware64 14.0 (desktop),
Posts: 8,357
|
+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>; }
|
|
|
|
03-11-2010, 08:42 PM
|
#12
|
|
Senior Member
Registered: Aug 2006
Posts: 2,695
|
Quote:
Originally Posted by catkin
+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
|
|
|
|
03-11-2010, 08:57 PM
|
#13
|
|
Moderator
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris10, Solaris 11, Ubuntu, OL
Posts: 9,311
|
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
|
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -5. The time now is 03:36 PM.
|
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|