LinuxQuestions.org
Go Job Hunting at the LQ Job Marketplace
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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



Reply
 
Search this Thread
Old 03-11-2010, 04:44 PM   #1
dbrazeau
Member
 
Registered: Aug 2009
Distribution: Fedora, OpenSuse, DENX Embedded Linux
Posts: 162

Rep: Reputation: 28
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
 
Old 03-11-2010, 04:46 PM   #2
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,415

Rep: Reputation: 1968Reputation: 1968Reputation: 1968Reputation: 1968Reputation: 1968Reputation: 1968Reputation: 1968Reputation: 1968Reputation: 1968Reputation: 1968Reputation: 1968
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?
 
Old 03-11-2010, 04:49 PM   #3
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris10, Solaris 11, Mint, OL
Posts: 9,523

Rep: Reputation: 365Reputation: 365Reputation: 365Reputation: 365
This should work:
Code:
echo "$RESULT" | grep "\."
Acid_kewpie was faster ...

Last edited by jlliagre; 03-11-2010 at 04:50 PM.
 
Old 03-11-2010, 04:49 PM   #4
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957
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 04:50 PM.
 
Old 03-11-2010, 04:51 PM   #5
dbrazeau
Member
 
Registered: Aug 2009
Distribution: Fedora, OpenSuse, DENX Embedded Linux
Posts: 162

Original Poster
Rep: Reputation: 28
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.
 
Old 03-11-2010, 04:53 PM   #6
dbrazeau
Member
 
Registered: Aug 2009
Distribution: Fedora, OpenSuse, DENX Embedded Linux
Posts: 162

Original Poster
Rep: Reputation: 28
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!
 
Old 03-11-2010, 04:53 PM   #7
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,415

Rep: Reputation: 1968Reputation: 1968Reputation: 1968Reputation: 1968Reputation: 1968Reputation: 1968Reputation: 1968Reputation: 1968Reputation: 1968Reputation: 1968Reputation: 1968
but you know your regex's right? "." means a single character, not a period. escape it - \. or use single quotes around it, not double.
 
Old 03-11-2010, 04:58 PM   #8
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris10, Solaris 11, Mint, OL
Posts: 9,523

Rep: Reputation: 365Reputation: 365Reputation: 365Reputation: 365
Quote:
Originally Posted by dbrazeau View Post
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 ...
 
Old 03-11-2010, 05:06 PM   #9
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,415

Rep: Reputation: 1968Reputation: 1968Reputation: 1968Reputation: 1968Reputation: 1968Reputation: 1968Reputation: 1968Reputation: 1968Reputation: 1968Reputation: 1968Reputation: 1968
please keep up. At least I replied too late within a minute!
 
Old 03-11-2010, 08:33 PM   #10
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 241Reputation: 241Reputation: 241
don't need to use external tools, in bash

Code:
$ string="1.23"
$ case $string in  *"."* ) echo "$string";; esac
 
Old 03-11-2010, 09:31 PM   #11
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Servers: Debian Squeeze and Wheezy. Desktop: Slackware64 14.0. Netbook: Slackware 13.37
Posts: 8,563
Blog Entries: 29

Rep: Reputation: 1179Reputation: 1179Reputation: 1179Reputation: 1179Reputation: 1179Reputation: 1179Reputation: 1179Reputation: 1179Reputation: 1179
+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>; }
 
Old 03-11-2010, 09:42 PM   #12
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 241Reputation: 241Reputation: 241
Quote:
Originally Posted by catkin View Post
+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
 
Old 03-11-2010, 09:57 PM   #13
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris10, Solaris 11, Mint, OL
Posts: 9,523

Rep: Reputation: 365Reputation: 365Reputation: 365Reputation: 365
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
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Grep variable with space brainlesseinstein Linux - General 3 08-10-2009 12:28 AM
Redirecting grep output to a variable dimako83 Linux - Software 6 11-17-2008 08:22 AM
Using a variable containing a filename in grep TrumpetMan258 Programming 2 03-01-2008 02:27 PM
Grep result passed to variable cornish Programming 5 11-15-2007 06:05 PM
How to use grep to search for a specific variable.. memo007 Linux - Software 4 02-24-2007 06:52 PM


All times are GMT -5. The time now is 04:02 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration