how to store grep command in a Variable


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting how to store grep command in a Variable
# 1  
Old 06-18-2008
how to store grep command in a Variable

I have a variable A

echo $A
5060

I am exporting the value X,Y,Z and it id fetching right thing and When I run

C=`${X} -l ${Y} ${Z} "trap '' INT;. ~/.profile >/dev/null 2>/dev/null; netstat -na | grep \$A`"

here it is going to same directory and also running netstat -na | grep 5060

echo $C

shows extra lines which is present in the directory with grep A value which is not actually grepped So how to avoid that value and output will come only 5060

But I need grep A value only
# 2  
Old 06-18-2008
What's in X, and why do you have a backslash before $A? Why do you execute X inside the backticks if you don't want its output to be included? Of course, you can redirect it just like you do with the .profile (again, the purpose of which remains unclear ... do you really want that inside the backticks?)

You have the basic syntax almost right, save for the backslash. Does the following not do what you want?

Code:
C=`netstat -na | grep "$A"`

# 3  
Old 06-18-2008
it is same as my old script but problem is that when after grep 5060 *.* is coming with grep value for that all things wgich are present in that script directory are greped in to that variable ..So I need only to avoid *.* So Pls suggest how to avoid ??
# 4  
Old 06-18-2008
Where is the *.* (because it's not in the commands you have posted)?

How are you using $C? I'm guessing the problem might be in that context actually.
# 5  
Old 06-18-2008
tHANKS
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Store command inside variable

Is it possible to store a command inside a variable? i want this piece to be stored inside a variable, so i can use it later in a different command $u | cut -d " " -f 2 var="$u | cut -d " " -f 2" eval $var I tried to use eval but I receive this error: -f 2: command not found ... (5 Replies)
Discussion started by: velos
5 Replies

2. Shell Programming and Scripting

How to store result of grep into a variable?

In a directory i have a file *output* whose contents are changing for every simulation. zgrep "trace file is" *output* | zgrep g o/p: trace file is Int_01.1352176388z4f56ec33.0.trace.gz I want to extract "Int_01.1352176388z4f56ec33.0.trace.gz" from the above result into a variable. i... (2 Replies)
Discussion started by: twistedpair
2 Replies

3. UNIX Desktop Questions & Answers

How to store the value of grep in a variable

Hi , I was trying to store the value of a grep command to store in a variable so i can use it somewhere else in my script but i am getting error, i am using bash shell in linux: var= `grep -n "$DATE" testfile.log | head -n 1 | sed -n 's/^\(*\).*/\1/p` echo "$var"I get the error: unexpected... (5 Replies)
Discussion started by: learninguser235
5 Replies

4. Shell Programming and Scripting

How to store the return value of a command into a variable?

I want to store the return value of grep -c string filename into a variable, say count. How do I do that? For example if grep -c "string" "filename" shows 0 on executing it in the sh shell then I want to store this 0 in a variable. Is it possible? :D (5 Replies)
Discussion started by: navienavnav
5 Replies

5. Shell Programming and Scripting

Using variable to store of find command

Hello Experts I am newbie to unix and writing one script to make archive files Problme i am facing is : I have used find command to find the type of files and I am storing find command results in a variable. When I echo the variable I can see that path is printed properly but when i am... (5 Replies)
Discussion started by: mitsyjohn
5 Replies

6. Shell Programming and Scripting

Not able to store command inside a shell variable, and run the variable

Hi, I am trying to do the following thing var='date' $var Above command substitutes date for and in turn runs the date command and i am getting the todays date value. I am trying to do the same thing as following, but facing some problems, unique_host_pro="sed -e ' /#/d'... (3 Replies)
Discussion started by: gvinayagam
3 Replies

7. Shell Programming and Scripting

grep attribute value pair and store it a variable

Hi, I have a file contains attribute value pair like.. ..name=erick rollno=583.0 pass=recon.. From the above line, i need to grep for only "rollno" and store "rollno=583.0" in a variable. Pls suggest (6 Replies)
Discussion started by: skraja1982
6 Replies

8. UNIX for Dummies Questions & Answers

Using GREP/AWK to extract a number and store it as a variable

Hello, I have a question regarding the awk command. Here is the line I need to grep: 1 F= -.13250138E+03 E0= -.13249556E+03 d E =-.174650E-01 mag= 35.2157 Instead of displaying the number in red I would like to store it as a variable such as X. Is there a way to do this? Thanks for any... (3 Replies)
Discussion started by: modey3
3 Replies

9. UNIX for Dummies Questions & Answers

How to Store command O/P to a variable...

Hi all.. I got a problem.. Its easy to redirect o/p to a file.. But Is it possible to redirect the O/P to a variable? For example: I've a command in my script: string1=cut -d ':' -f2 file.txt When I do: echo $string1 The value is empty... Pls suggest me how to store the value... (7 Replies)
Discussion started by: smartbuddy
7 Replies

10. Shell Programming and Scripting

Grep results to store in a shell variable

I have the results of a grep with -n store in a shell variable ie VAR=`grep -n -e 'PATTERN' file` How ever I am missing the line breaks in the variable , How do I store the resualts of grep with many lines in to a variables. I want the varable should be the sawmway as we do the grep grep... (3 Replies)
Discussion started by: jojan
3 Replies
Login or Register to Ask a Question