Put the output of grep in a variable


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Put the output of grep in a variable
# 1  
Old 04-23-2008
Put the output of grep in a variable

Hi,
in a shell script how can I put the result of a grep command in a variable :
myvariable=grep mystring myfilename

Thank you.
# 2  
Old 04-23-2008
myvariable=$(grep mystring myfilename)
# 3  
Old 04-23-2008
Hmmm...

I would have said:

myvariable=`grep mystring myfilename`
# 4  
Old 04-23-2008
Thanks to all.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

I must use first sentence from a file to put into variable

i am having some bash script which must use first sentence of the file. For example i have file which content is: test 213 So I must use word test into my bash script, and put it into variable. I am using a one variable named value value=$(</home/rusher/test.txt) so instead using test.txt... (1 Reply)
Discussion started by: tomislav91
1 Replies

2. Shell Programming and Scripting

Output of a Grep(Which already has a variable) into a Variabale

Hi All, I have a an array, in which i need to loop through all the variable and check each variable individually for a pattern. For ex #!/bin/ksh set -A FileNames $(</Temp/AllNames) echo "Total array elements :" ${#FileNames } for i in ${FileNames} do # if the "${i} | grep Last"... (3 Replies)
Discussion started by: baranisachin
3 Replies

3. Shell Programming and Scripting

ls | grep (i dont know what to put here)

Dear users, I googled for a while, but i have got a lot of different answers regarding a simple unix command. lets say there are a lot of files in a directory. How can i list the files in a directory whose file types is "text"? Thank you in advance (4 Replies)
Discussion started by: kevincobain2000
4 Replies

4. Shell Programming and Scripting

How to put output of one command into a variable

Hi, Let say I have these 3 files (state, list and myscript). I want to be able get the sample output like below when I run myscript. Any one know how to fix the code? TIA. ~~~~~~~~~~~~~~~ > cat /home/state CA > cat /home/list CA 100 50 20 AUS 120 61 10 > cat myscript... (6 Replies)
Discussion started by: joker_789us
6 Replies

5. Shell Programming and Scripting

Put output into an array

I'm trying to take the output of an environment that has multiple strings ex. # echo $SSH_CLIENT 192.168.1.1 57039 22 I need that IP... so I can set it to another environment. Thank you (3 Replies)
Discussion started by: adelsin
3 Replies

6. Shell Programming and Scripting

Problem with assigning output of grep + awk to a variable

Hi All, I am getting the output for the following command when i run it on the unix console. --------------------------- grep `whoami` /etc/passwd | awk '{print ($1);}' | cut -d ":" -f3 ---------------------------- But i made it into a script and tried to print the variable, its... (5 Replies)
Discussion started by: meheretoknow
5 Replies

7. Shell Programming and Scripting

redirect the grep output into the variable

i want to redirect the grep output into the variable but i am not able to get it i tried veri=`grep -i $1 "${logdir}"* | grep -i adding | grep -iv equation | tail -1 | cut -d ':' -f 1` vari=$(grep -i $1 "${logdir}"* | grep -i adding | grep -iv equation | tail -1 | cut -d ':' -f 1) BUT NOT... (1 Reply)
Discussion started by: mail2sant
1 Replies

8. AIX

Put one ligne from a file a variable

Hi everybody I looking the put the result of a commane to a Variable i explain here is my command: FJTS_UK:root:common@ukaix3:/> cat sortie | grep "^"| awk '{ print $1}' 15 FJTS_UK:root:common@ukaix3:/> sortie is a texte file I want to put the result of commande in a... (1 Reply)
Discussion started by: kykyboss
1 Replies

9. UNIX for Dummies Questions & Answers

How to store output in variable when put in background

Hi, How do I store following command output: export RESULT=`date` & It works when I do : export RESULT=`date` But what I need is when command put it background, I also need that output going to RESULT variable. Is there any way ? Thanks Sanjay (1 Reply)
Discussion started by: sanjay92
1 Replies

10. UNIX for Dummies Questions & Answers

capturing the output of grep as integer variable

Hi, I have an expression using grep and nawk that captures the ID number of a given Unix process. It gets printed to screen but I don't know how to declare a variable to this returned value! For example, ps -ef|grep $project | grep -v grep | nawk '{print $2}' This returns my number. How... (2 Replies)
Discussion started by: babariba
2 Replies
Login or Register to Ask a Question