Grepping for variable in brackets


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Grepping for variable in brackets
# 1  
Old 03-08-2013
Grepping for variable in brackets

I have a script which reads a number out of a log file. The pertinent line is this:

cat /tmp/listofnumbers

When I run cat /tmp/listofnumbers what I am seeing is [9878] [6376] on each line.
I am trying to make the script read from that file and grep for a variable like the following line should do:

Code:
cat /tmp/listofnumbers |grep "\[$var\]"

But that doesn't work. I can echo the variable number but the grep will never return it in the file. I have tried using $( and other tricks but it doesn't work. Any suggestions would be greatly appreciated.
# 2  
Old 03-08-2013
How do you set the $var variable? I suspect the 'var' contains some other characters, since the grep line works as expected:
Code:
$ var=9878; echo '[9878] [6376]' | grep "\[$var\]"
[9878] [6376]

If you post your script, you would have a much better chance of getting help.
# 3  
Old 03-08-2013
Solved!

Thanks for your suggestion, but I did get it solved:
I tried this:
Code:
cat /usr/openv/netbackup/logs/bptm/log.030713 |egrep "\[$bpx\]"

egrep worked for me don't know why grep didn't.
# 4  
Old 03-08-2013
Quote:
Originally Posted by newbie2010
Thanks for your suggestion, but I did get it solved:
I tried this:
Code:
cat /usr/openv/netbackup/logs/bptm/log.030713 |egrep "\[$bpx\]"

egrep worked for me don't know why grep didn't.
I think you're kidding yourself.

The grep and egrep utilities should treat these expressions the same way. We need to see how you are setting bpx (or in your earlier example var) and a sample of the lines in your input file that you expect the expression to match to figure our what is really going on here.

And, for the record:
Code:
cat file|grep expression

is much more efficiently written as:
Code:
grep expression file

# 5  
Old 03-08-2013
It should work, as mirni demonstrates. Here is another demonstration.
Code:
$ cat temp.x
[123]
[456]
$ x=123
$ cat temp.x | grep "\[$x\]"
[123]
$ cat temp.x | grep -v "\[$x\]"
[456]

I don't think it's a good idea to say "grep doesn't work, but don't know why, but egrep works for some unknown reason, so I'll use egrep". Better to under WHY grep doesn't work for you. There is always a reason. I hope you can find the reason.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Grepping for one variable while using awk to parse an associated variable

Im trying to search for a single variable in the first field and from that output use awk to extract out the lines that contain a value less than a value stored in another variable. Both the variables are associated with each other. Any guidance is appreciated. File that contains the... (6 Replies)
Discussion started by: ncwxpanther
6 Replies

2. Shell Programming and Scripting

How to handle grepping variable data containing wildcards?

I have a lot of files with keywords and unique names. I'm using a shell script to refer to a simple pattern file with comma separated values in order to match on certain keywords. The problem is that I don't understand how to handle the wildcard values when I want to skip over the unique names. ... (5 Replies)
Discussion started by: abercrom
5 Replies

3. Shell Programming and Scripting

Grepping the file into a variable after SSH into a server

Hi All, When i am logged into a server , i am able to assign grep value to a variable as follows. VAR=`grep 'Listener stopped' /logs/perf.log` However, when i log out of the server, and try to execute the following command by first SSHing into server, it throws error. $ VAR=`ssh Server... (4 Replies)
Discussion started by: srkmish
4 Replies

4. Shell Programming and Scripting

set variable with square brackets in c shell!

Hi Guys, I want to set a variable which happend to have square brackets in them. I tried multiple ways that is by escaping it with quotes (both single and double ) but it didn't help. All I want is: set x = slicearray.slice\.post My failed attempts were: set x = "slicearray.slice\.post"... (3 Replies)
Discussion started by: dixits
3 Replies

5. Shell Programming and Scripting

Shell Variable in Curly Brackets Returns Empty Value

Hello Team, I have a script which will grep for a time from a file. I have following code to grep for a time in a file. node=`hostname` current_date=`date` file11=weblogic.log next_date=`date '+%b %e, %Y'` next_date_time11=`grep -i "${#next_date}" ${file11}| tail -1 | awk... (3 Replies)
Discussion started by: coolguyamy
3 Replies

6. Shell Programming and Scripting

Delete text between square brackets and also delete those square brackets using sed or awk

Hi All, I have a text file which looks like this: computer programming systems engineering I want to get rid of these square brackets and also the text that is inside these brackets. So that my final text file looks like this: computer programming systems engineering I am using... (3 Replies)
Discussion started by: shoaibjameel123
3 Replies

7. Shell Programming and Scripting

Grepping file and returning passed variable if the value does not exist in file at all.

I have a list of fields that I want to check a file for, returning that field if it not found at all in the file. Is there a way to do a grep -lc and return the passed variable too rather then just the count? I am doing some crappy work-around now but I was not sure how to regrep this for :0 so... (3 Replies)
Discussion started by: personalt
3 Replies

8. Shell Programming and Scripting

Get value between brackets

Hi I am having hard time getting this right and need some help. I Have several log files, and everyone contains the following 3 lines at the end: 4 ETW000 Disconnected from database. 4 ETW000 End of Transport (0000). 4 ETW000 date&time: 13.01.2011 - 08:03:28 I need to capture the value... (7 Replies)
Discussion started by: nimo
7 Replies

9. Shell Programming and Scripting

Grepping Date Variable

Hello, I would like for the user to input the date for a particular log file, then have the input sent to a variable, which is then used via grep to extra the logs for the specific date the user request. I did some searching, but I still don't understand why I'm not seeing any result. ... (4 Replies)
Discussion started by: ravzter
4 Replies

10. UNIX for Dummies Questions & Answers

grepping a variable

I need to pass a parameter that will then be grepped. I need it to grep /paramater and then have a space so if 123 was passed my grep would be grep '/123 ' sample.log this works fine from the command line but when i try and set it searchThis="/$2 " and then run grep $searchThis... (6 Replies)
Discussion started by: magnia
6 Replies
Login or Register to Ask a Question