LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 03-27-2015, 11:12 PM   #1
nine4409
LQ Newbie
 
Registered: Mar 2015
Posts: 1

Rep: Reputation: Disabled
Question How can I grep variable?


I want to And search grep shell

but It's hard to grep variable


---------------------------------------------------------------
#!/bin/bash


if [ $# -eq 0 ]
then
echo "Ussage: phone searchfor [...searchfor]"
echo "(You didn't tell me what you want to search for )"

else

pass=0
find=""

for idx in $*
do
if [ -n "$idx" ]
then
if [ $pass -eq 0 ]
then
find=$(egrep "$idx" mydata)
pass=1

else

find=$("$find" | grep "$idx")

echo $find
fi
fi

done

if [ -z "$find" ]
then
echo "There is no such thing"
else

echo $find | awk -f display.awk

fi

fi

-----------------------------------------------------

there is one error : command not found

in find=$("$find" | grep "$idx")

how can I grep variable and store it into variable?
 
Old 03-28-2015, 03:57 AM   #2
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,256

Rep: Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686
Unless $find is a command I am not how you think the pipe will work? Maybe try calling a command on the variable, say echo, then there is some standard input for grep and the pipe to work with.

Also, what would be the point? Is it likely that $find will have multiple lines in it? If not, why use grep?
 
Old 03-30-2015, 02:11 AM   #3
sharky
Member
 
Registered: Oct 2002
Posts: 478

Rep: Reputation: 47
Not sure what you want but maybe you could just run env and then grep.

Code:
env | grep <whatever>
or if you want to list just the environment variables without the values;

Code:
env | awk -F"=" '{print $1}' | grep <whatever>
 
  


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
how to pass a variable to grep mkanwischer Linux - Newbie 5 12-03-2013 10:36 PM
[SOLVED] assign full name to variable then grep a file for the variable socalheel Programming 3 09-16-2013 12:04 PM
grep variable question graphicsmanx1 Programming 11 12-06-2012 03:16 PM
How do you grep a variable? dbrazeau Programming 12 03-11-2010 09:57 PM
Using a variable containing a filename in grep TrumpetMan258 Programming 2 03-01-2008 02:27 PM


All times are GMT -5. The time now is 11:05 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration