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. |
|
 |
02-02-2009, 08:50 PM
|
#1
|
|
Senior Member
Registered: Feb 2004
Location: Middle of nowhere
Distribution: Debian Squeeze
Posts: 1,249
Rep:
|
grep a shell script variable contents
Hi,
I have a bash shell script which has a variable $DEVICE. Now I would like to use "grep" to do some pattern matching on the contents of variable $DEVICE.
For example, suppose variable DEVICE has "ABC XYZ" stored in it. Now I would like to grep "ABC" on $DEVICE.
I am not ale to figure out how to do it. It is easy to do a grep on file:
grep "ABC" <file-name> but how do you do on a variable?
Thanks
|
|
|
|
02-02-2009, 09:11 PM
|
#2
|
|
ReliaFree Maintainer
Registered: Aug 2004
Location: Kalamazoo, Michigan
Distribution: Slackware, Cross Linux from Scratch, Gentoo
Posts: 2,663
Rep: 
|
One way would be...
Code:
echo $DEVICE | grep -o ABC
Someone else will have another that is probably better.
|
|
|
|
02-02-2009, 11:36 PM
|
#3
|
|
Senior Member
Registered: Mar 2007
Location: Russia
Distribution: Slackware 12.2
Posts: 1,202
Rep:
|
Quote:
Originally Posted by kushalkoolwal
Hi,
I have a bash shell script which has a variable $DEVICE. Now I would like to use "grep" to do some pattern matching on the contents of variable $DEVICE.
For example, suppose variable DEVICE has "ABC XYZ" stored in it. Now I would like to grep "ABC" on $DEVICE.
I am not ale to figure out how to do it. It is easy to do a grep on file:
grep "ABC" <file-name> but how do you do on a variable?
Thanks
|
You can't get value from $DEVICE using grep. You can extract contents of assignment operation using something like
Code:
grep "^\$DEVICE[ \t]*=[ \t]*.*$" file.txt| sed 's/^\$DEVICE[ \t]*=[ \t]\(.*\.)$/\1/g'
(grep finds line with assignment, sed cuts off left part of assignment. Probably can be made 2x times shorter)
But you can't extract value from script without running it, because if there are operations that change value of $DEVICE later, grep/sed won't handle it, and you will get incorrect result.
Last edited by ErV; 02-02-2009 at 11:39 PM.
|
|
|
|
02-02-2009, 11:38 PM
|
#4
|
|
Senior Member
Registered: Feb 2004
Location: Middle of nowhere
Distribution: Debian Squeeze
Posts: 1,249
Original Poster
Rep:
|
Quote:
Originally Posted by weibullguy
One way would be...
Code:
echo $DEVICE | grep -o ABC
Someone else will have another that is probably better.
|
Thanks that just worked for me! Exactly what I was looking for...
|
|
|
|
02-03-2009, 04:35 AM
|
#5
|
|
Member
Registered: Nov 2003
Location: Edinburgh, UK
Distribution: Arch
Posts: 34
Rep:
|
Why not use the pattern-matching capabilities of parameter expansion in bash?
Call this little script "tryme":
Quote:
#!/bin/bash
vartest=${1/*ABC*}
if [ "x$vartest" == "x" ];
then
echo vartest matches
else
echo vartest does not match
fi
|
Then, assuming you have made the script executable:
Quote:
% ./tryme BBBABC
vartest matches
% ./tryme "BBB ABC"
vartest matches
% ./tryme "BBB ABBC"
vartest does not match
% ./tryme "..ABCXY"
vartest matches
% ./tryme "..A BCXY"
vartest does not match
%
|
|
|
|
|
02-03-2009, 07:47 AM
|
#6
|
|
Senior Member
Registered: Oct 2004
Location: Houston, TX (usa)
Distribution: MEPIS, Debian, Knoppix,
Posts: 4,727
|
OP,
It looks like you've got your answer, but I'm curious: What problem prompted your Q?
Using grep -o, Post #2
weibullguy,
Nice piece of code. grep -o is one of my favorites. (Who was the 6th Marx brother? . . . Greppo)
Using grep & sed
Quote:
Originally Posted by ErV
You can extract contents of assignment operation using something like
Code:
grep "^\$DEVICE[ \t]*=[ \t]*.*$" file.txt| sed 's/^\$DEVICE[ \t]*=[ \t]\(.*\.)$/\1/g'
|
- I afraid the above misses the point of the OP.
- This code won't work because bash doesn't use '$' as part of the variable name in assignment statements.
Code:
grep "^DEVICE[ \t]*=[ \t]*.*$" file.txt| sed 's/^DEVICE[ \t]*=[ \t]\(.*\.)$/\1/g'
- The '[ \t]*' surrounding the '=' is not only unnecessary, but wrong:
Code:
$ DDD = xxx
bash: DDD: command not found
$ DDD= xxx
bash: xxx: command not found
$ DDD =xxx
bash: DDD: command not found
$ DDD=xxx
There must be no spaces in a bash variable assignment.
Code:
grep "^DEVICE=.*$" file.txt| sed 's/^DEVICE=\(.*\.)$/\1/g'
- There is no need to pipe grep to sed -- sed has its own line selection mechanism.
Code:
sed '/^DEVICE=/s/^DEVICE=\(.*\.)$/\1/g' file.txt
- The 2nd '.' in '\(.*\.)' is unnecessary & misplaced.
Code:
sed '/^DEVICE=/s/^DEVICE=\(.*\)$/\1/g' file.txt
- Escaping '(' & ')' can be avoided by using the "-r" option.
Code:
sed -r '/^DEVICE=/s/^DEVICE=(.*)$/\1/g' file.txt
- Commas are better delineators.
Code:
sed -r '/^DEVICE=/s,^DEVICE=(.*)$,\1,g' file.txt
- The 'g' in 's...g' is only necessary for multiple instaces in a given line, the '^' & '$' framing the search pattern preclude this possibility.
Code:
sed -r '/^DEVICE=/s,^DEVICE=(.*)$,\1,' file.txt
- If your intent is to print only the assigned values, you will need to include the "-n" & ",p" options:
Code:
sed -nr '/^DEVICE=/s,^DEVICE=(.*)$,\1,p' file.txt
- Finally, there is no need for the '\1' mechanism (& w/ it the "-r" option), just suppress the 'DEVICE=':
Code:
sed -n '/^DEVICE=/s,^DEVICE=,,p' file.txt
Warning: I did test this code, but only a little.
|
|
|
|
02-03-2009, 11:41 AM
|
#7
|
|
LQ Newbie
Registered: Feb 2007
Location: USA/Canada
Distribution: Red Hat, CentOS, Scientific, Fedora, Ubuntu, SUSE, SLES
Posts: 14
Rep:
|
pattern matching
If you wanted to use grep's pattern matching capability you could use
if (( `echo $variable | grep -c "PATTERN"` > 0 ))
then
CODE
fi
A better way could be to use Bash's pattern matching using =~ operator.
if [[ $variable =~ Regular Exp ]]
then
CODE
fi
|
|
|
|
02-03-2009, 12:15 PM
|
#8
|
|
ReliaFree Maintainer
Registered: Aug 2004
Location: Kalamazoo, Michigan
Distribution: Slackware, Cross Linux from Scratch, Gentoo
Posts: 2,663
Rep: 
|
Quote:
Originally Posted by archtoad6
Who was the 6th Marx brother?
|
Manfred, I think.
|
|
|
|
02-04-2009, 06:15 AM
|
#9
|
|
Senior Member
Registered: Oct 2004
Location: Houston, TX (usa)
Distribution: MEPIS, Debian, Knoppix,
Posts: 4,727
|
 , no it was Greppo, I hid the answer here (& in my OP) in a background matching color tag -- highlight it to read it; or did you know that?
... Of course you knew that, quoting the post displayed the hidden answer & the method of hiding it.
Last edited by archtoad6; 02-04-2009 at 06:20 AM.
Reason: fix color tag
|
|
|
|
| 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 07:13 AM.
|
|
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
|
|