grep in a variable


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting grep in a variable
# 1  
Old 05-29-2012
grep in a variable

Hello,

I usually search extensively and have to date found what I've needed. However, this one's got me stumped. I need to create a variable as follow. The issue however is that upon execution, it freezes. $var1 isn't always present in usage.log and this is fine but I'd like it to continue with the script if no match is found. Any ideas? Thanks.
Code:
var2=`grep $var1 /conf/usage.log | tail -1 | cut -d " " -f 2`


Last edited by joeyg; 05-29-2012 at 03:26 PM.. Reason: wrap commands and data with CodeTags
# 2  
Old 05-29-2012
try this -
Code:
var2=`cat /conf/usage.log|grep $var1| tail -1 | cut -d" " -f2`

# 3  
Old 05-29-2012
Thanks, that did the trick but error control leaves much to be desired Smilie
# 4  
Old 05-29-2012
Have you researched the
Code:
>2

option for error handling?
redirect errors somewhere. Two examples:
Code:
cat myfile 2>/dev/null
cat myfile 2>>/logfile.txt

The first send errors no-where
The second, appends to the end of a file called logfile.txt
# 5  
Old 05-29-2012
No, good point. I'll read into this. Thanks.
# 6  
Old 05-29-2012
The problem with the freezing is not freezing, but grep waiting for input. This is the case when var1 is empty and grep then thinks that "/conf/usage.log" is the search string. This can be avoided by double quoting var1 (which is good practice anyway):
Code:
var2=`grep "$var1" /conf/usage.log | tail -1 | cut -d " " -f 2`

In any case, you should still test for var1 being empty, because otherwise the last line of /conf/usage.log ends up in var2, no matter what it consists of...

Last edited by Scrutinizer; 05-29-2012 at 04:23 PM..
# 7  
Old 05-29-2012
grep not finding the pattern is not really an error, but grep succeeding in finding nothing. Just test for null before using it. Break it into separate steps depending on how granular you need to get to handle various conditions. It depends on your requirements. For instance, do you halt if $var1 is not set? Halt if $var1 is set but not found in the log? i.e. only grep if $var1 is not null:
Code:
#  Test to see if $var1 is null
if [[ -v "$var1" ]]; then  # Make sure to quote it so the test "sees" the null.
  echo '$var1 is null'
  # Code for var1 being null goes here.
else
  search=`grep $var1 /conf/usage.log`
  if [[ -v "$search" ]]; then  
    echo "[$var1] is not found"
    # Code for var1 not found goes here
  else  
    var2=`echo $search | tail -1 | cut -d " " -f 2` 
  fi
fi

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How to use $variable in grep?

hi i have a file which contains some messages counters. below is the snippet on the file. 17-05-29::22:36:21|message|231 17-05-29::22:36:31|message|222 17-05-29::22:36:41|message|213 17-05-30::22:36:51|message|221 17-05-30::22:37:01|message|227 17-05-30::22:37:11|message|207... (5 Replies)
Discussion started by: scriptor
5 Replies

2. Shell Programming and Scripting

Help grep one variable over other

Hi, I am trying to grep one variable over the other variable Example: i=abc j=ab grep $j $i I am getting this error: The error is due to $i being variable and not file. I know I could do it by putting the value of abc in a file and then greping it. (1 Reply)
Discussion started by: pinnacle
1 Replies

3. Shell Programming and Scripting

grep variable

I've got a file that I'm trying to grep through that looks like this: alpha1 alpha2 alpha3 beta1 beta2 gamma5 gamma6 gamma7 gamma8 gamma9 and I want the output to only contain the line with the highest value for each, so the output I want is: alpha3 beta2 gamma9 I also need... (11 Replies)
Discussion started by: tiberione
11 Replies

4. Shell Programming and Scripting

grep using variable

I have a pattern like: column "5" is missing PS: the no is in double quotes. The number usally changes, so we use a loop to grep. grep 'column "$number" is missing' filename.txt But it is not working.... How to solve this? (2 Replies)
Discussion started by: karumudi7
2 Replies

5. Shell Programming and Scripting

Grep through a variable

I want to search a text in file but that file is pointing to variable. ex: file=there was nothing special grep "there was nothing" $file but its not working . Can u let me know that how we can use variable($file) in grep command. Please use code tags (6 Replies)
Discussion started by: allthanksquery
6 Replies

6. Shell Programming and Scripting

grep using variable

how can I use grep with a variable to find a value? cat data.out Hello World grep "Hello World" data.out Hello World ## Value found I want to do something like this but can't seem to get it to work any suggestions would be appreciated. var="Hello World" grep $var data.out (3 Replies)
Discussion started by: BeefStu
3 Replies

7. Shell Programming and Scripting

grep a variable

can i grep a variable say i have a variable var=`hostname` and I want to make an if statement like if grep "esp-ueh" $var;then...... how can i do this I dont want to store this variable in a file and the grep it because my script will be used at the same time on multiple stations and then that... (9 Replies)
Discussion started by: lassimanji
9 Replies

8. Shell Programming and Scripting

grep a variable

Hi all, I am trying to do a simple thing in my mind. However I am fairly new to bash. What I need to do is create a folder for each partition on each CD, and each partition has a unique name (with spaces in it, do not ask why, it is already done :confused: ) . All CD's will show up... (2 Replies)
Discussion started by: sgstuart
2 Replies

9. Shell Programming and Scripting

grep with variable

Hi, I can't get this script to work (returns 0, should return 3): $ cat A.lst | \ while read LINE do echo "$LINE" grep -c "$LINE" B.tmp done> > > > > Socket 0 $ but in contrast this one works fine (returns 3 as expected): $ LINE=Socket $ grep -c $LINE B.tmp 3 $ (5 Replies)
Discussion started by: ozvena
5 Replies

10. Shell Programming and Scripting

How to grep a variable?

Hi, I'd like to grep a variable that I saved in the program. Like grep '0\$variable1' file1 Does someone know what's wrong with this command? Thanks a lot! (2 Replies)
Discussion started by: whatisthis
2 Replies
Login or Register to Ask a Question