LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
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, 10: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, 02:57 AM   #2
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,008

Rep: Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193
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, 01:11 AM   #3
sharky
Member
 
Registered: Oct 2002
Posts: 569

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



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

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 12:27 PM.

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
Open Source Consulting | Domain Registration