grepping a variable

Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers grepping a variable
# 1  
Old 07-23-2009
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 $logfile

it doesnt work, any ideas?
# 2  
Old 07-23-2009
It doesn't work because it returns too many lines, or no lines?

You should enclose the grep expression in quotes:

grep "$searchThis" $logfile

if you expect it to add the space to the search.
# 3  
Old 07-23-2009
returns nothing


searchParam="/$1 "
grep "$searchParam" $logfile
# 4  
Old 07-23-2009
What is $1? Are you running this grep in a script? (you said $2 in your original post)

Show me how you run the script (i.e. if you pass $searchParam to the script, do you quote that (i.i.e mySctipt "$searchParam")).
# 5  
Old 07-23-2009
this is the full script, its going into an infinite loop



searchParam="/$1 "
grep "$searchParam" $logfile
# 6  
Old 07-23-2009
That's no surprise, really.

The variable has a capital F and the grep command doesn't:

searchParam="/$1 "
grep "$searchParam" $logfile

It hasn't gone into an infinite loop... it's just waiting for input because $logfile is blank the shell evaluates that to nothing (not a filename as you thought).
# 7  
Old 07-23-2009
It is a good practice to check for uninitialized variables in a shell.

$logfile would have hopefully been caught in that.

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

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 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: ... (4 Replies)
Discussion started by: newbie2010
4 Replies

5. 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

6. 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

7. Shell Programming and Scripting

Please help on grepping

Hi All, I have a log file and I want to parse the logfile with a script.A sample text is shown below: I would grep on "return code " on this file. Any idea how the lines above and below the grep patterns could also be extracted. Thanks! nua7 The runLoggingInstall return code is 0... (3 Replies)
Discussion started by: nua7
3 Replies

8. Shell Programming and Scripting

Grepping issue..

I found another problem with my disk-adding script today. When looking for disks, I use grep. When I grep for the following disk sizes: 5242880 I also pick up these as well: 524288000 How do I specifically pick out one or the other, using grep, without resorting to the -v option? ... (9 Replies)
Discussion started by: LinuxRacr
9 Replies

9. Shell Programming and Scripting

grepping around

Using shell scripts, I use grep to find the word “error” in a log file: grep error this.log. How can I print or get the line 3 lines below the line that word “error” is located? Thanks in advance for your response. (9 Replies)
Discussion started by: cbeauty
9 Replies

10. UNIX for Dummies Questions & Answers


Is there a way to grep for something and then print out 10 lines after it. for example if I want to grep for a word, then output the following 10 or whatever number of lines after the word. (5 Replies)
Discussion started by: eloquent99
5 Replies
Login or Register to Ask a Question