LinuxQuestions.org
Share your knowledge at the LQ Wiki.
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 02-02-2009, 08:50 PM   #1
kushalkoolwal
Senior Member
 
Registered: Feb 2004
Location: Middle of nowhere
Distribution: Debian Squeeze
Posts: 1,249

Rep: Reputation: 48
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
 
Old 02-02-2009, 09:11 PM   #2
weibullguy
ReliaFree Maintainer
 
Registered: Aug 2004
Location: Kalamazoo, Michigan
Distribution: Slackware-current, Cross Linux from Scratch, Gentoo
Posts: 2,701
Blog Entries: 1

Rep: Reputation: 219Reputation: 219Reputation: 219
One way would be...
Code:
echo $DEVICE | grep -o ABC
Someone else will have another that is probably better.
 
Old 02-02-2009, 11:36 PM   #3
ErV
Senior Member
 
Registered: Mar 2007
Location: Russia
Distribution: Slackware 12.2
Posts: 1,202
Blog Entries: 3

Rep: Reputation: 62
Quote:
Originally Posted by kushalkoolwal View Post
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.
 
Old 02-02-2009, 11:38 PM   #4
kushalkoolwal
Senior Member
 
Registered: Feb 2004
Location: Middle of nowhere
Distribution: Debian Squeeze
Posts: 1,249

Original Poster
Rep: Reputation: 48
Quote:
Originally Posted by weibullguy View Post
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...
 
Old 02-03-2009, 04:35 AM   #5
Dr_P_Ross
Member
 
Registered: Nov 2003
Location: Edinburgh, UK
Distribution: Arch
Posts: 41

Rep: Reputation: 18
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
%
 
Old 02-03-2009, 07:47 AM   #6
archtoad6
Senior Member
 
Registered: Oct 2004
Location: Houston, TX (usa)
Distribution: MEPIS, Debian, Knoppix,
Posts: 4,727
Blog Entries: 15

Rep: Reputation: 230Reputation: 230Reputation: 230
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 View Post
You can extract contents of assignment operation using something like
Code:
grep "^\$DEVICE[ \t]*=[ \t]*.*$" file.txt| sed 's/^\$DEVICE[ \t]*=[ \t]\(.*\.)$/\1/g'
  1. I afraid the above misses the point of the OP.

  2. 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'
  3. 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'
  4. 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
  5. The 2nd '.' in '\(.*\.)' is unnecessary & misplaced.
    Code:
    sed '/^DEVICE=/s/^DEVICE=\(.*\)$/\1/g' file.txt
  6. Escaping '(' & ')' can be avoided by using the "-r" option.
    Code:
    sed -r '/^DEVICE=/s/^DEVICE=(.*)$/\1/g' file.txt
  7. Commas are better delineators.
    Code:
    sed -r '/^DEVICE=/s,^DEVICE=(.*)$,\1,g' file.txt
  8. 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
  9. 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
  10. 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.
 
Old 02-03-2009, 11:41 AM   #7
Farzan Mufti
LQ Newbie
 
Registered: Feb 2007
Location: USA/Canada
Distribution: Red Hat, CentOS, Scientific, Fedora, Ubuntu, SUSE, SLES
Posts: 14

Rep: Reputation: 6
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
 
Old 02-03-2009, 12:15 PM   #8
weibullguy
ReliaFree Maintainer
 
Registered: Aug 2004
Location: Kalamazoo, Michigan
Distribution: Slackware-current, Cross Linux from Scratch, Gentoo
Posts: 2,701
Blog Entries: 1

Rep: Reputation: 219Reputation: 219Reputation: 219
Quote:
Originally Posted by archtoad6 View Post
Who was the 6th Marx brother?
Manfred, I think.
 
Old 02-04-2009, 06:15 AM   #9
archtoad6
Senior Member
 
Registered: Oct 2004
Location: Houston, TX (usa)
Distribution: MEPIS, Debian, Knoppix,
Posts: 4,727
Blog Entries: 15

Rep: Reputation: 230Reputation: 230Reputation: 230
, 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
 
  


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
Comparing two files to get matched contents in another file using shell script pooppp Linux - Networking 3 08-05-2008 12:11 AM
Shell script to grep log for a specific error sfcg Programming 8 06-28-2008 09:12 AM
Shell script with grep MicahCarrick Programming 4 08-15-2006 01:08 PM
Grep Syslog - email shell script voodoofxz Linux - Newbie 1 09-06-2005 04:27 PM
grep in shell script fails on redhat 3.0 rlangsto Linux - General 4 03-06-2005 07:42 PM


All times are GMT -5. The time now is 10:31 AM.

Main Menu
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