bash: using a string variable in grep


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting bash: using a string variable in grep
# 1  
Old 07-11-2012
bash: using a string variable in grep

Hi,

I've been stuck for several days on this. Using grep on a command line, I can use quotes, eg...
Code:
grep 'pattern of several words' filename

I want to do this in my bash script. In my script I have captured the several command line arguments (eg arg1 arg2) into a variable:
Code:
variable=$@

I guess this is an array variable consisting of arg1, arg2 etc. I try to use it later on data piped into grep...
Code:
cat file | grep $variable

This doesn't work -- grep sees arg1 and arg2 of my variable separately and interprets it as 'grep pattern[arg1] filename[arg2]'. Consequently it errors saying "file [arg2] not found".

So I tried using quotes on the script line, as I would on the command line...
Code:
cat file | grep '$variable'

but it then seems to treat $variable literally and greps on the actual name of the string, so returns no results.

I've tried backslashed quotes....
Code:
cat file | grep \'$variable\'

but that doesn't work either, it thinks arg2 is a filename again.

Any ideas on the way to do this?

Thanks,
Adrian.

Last edited by Franklin52; 07-11-2012 at 05:36 PM.. Reason: Please use code tags for data and code samples, thank you
# 2  
Old 07-11-2012
You missed the good old double-quotes....

Code:
fgrep "$variable" file

assuming that you need to search the string as a string and not as a regular expression. Drop the "f" if you want to search with a regular expression.

Last edited by elixir_sinari; 07-11-2012 at 10:34 AM..
# 3  
Old 07-11-2012
Thanks, that worked!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Grep a sub-string from a string stored in a variable.

For example: I am grepping "Hello" from a file and there are 10 matches. So all ten lines with match will get stored into a variable($match). Now I want to ignore those lines which have "Hi" present in that. Currently I tried this: match = grep "Hello" file | grep -v "Hi" file But that's not... (2 Replies)
Discussion started by: pavan
2 Replies

2. Shell Programming and Scripting

Convert a string to variable in Bash shell

Hi All; I have 2 variable let's say $A and $B. These are actually some remotely executed command outputs. I captured these values in my local variables. Here is my problem. When I try to do some arithmetic operation with these variables, I receive an error message. Neither expr nor typeset -i... (3 Replies)
Discussion started by: Meacham12
3 Replies

3. Shell Programming and Scripting

Passing string as variable(s) in bash

I'm trying to write a basic bash script that takes input you give (what directory, if any, what name, if any ....) and passes the information to find. I'm trying to just create a string with all variables and then pass it to find. So far I have this extremely simple: #!/bin/bash -f ... (2 Replies)
Discussion started by: Starting_Leaf
2 Replies

4. Shell Programming and Scripting

Bash string variable outputting incorrectly

Hello All, I am learning BASH scripting and I would appreciate any help with a small problem I am having... I am writing a script that builds a simple hosts file for DNS reasons related to a piece of software called netdb by parsing another application's config files for IP's and their... (4 Replies)
Discussion started by: Wesley545
4 Replies

5. UNIX for Dummies Questions & Answers

bash variable string declaration

Hello, Why is this not working in a script? files="test.fsa" echo $files for file in $files do if then echo "$file does not exist." fi run a command done I get an error saying (3 Replies)
Discussion started by: verse123
3 Replies

6. Shell Programming and Scripting

(BASH) Using a loop variable to grep something in a file?

Hi, I have a loop running until a variable L that is read previously in the full script. I'd like to grep some information in an input file at a line that contains the value of the loop parameter $i. I've tried to use grep, but the problem is nothing is written in the FILE files. It seems grep... (5 Replies)
Discussion started by: DMini
5 Replies

7. Shell Programming and Scripting

Bash assign string to variable

Hi ,I am trying to assign string to variable ,but it doesn't work Also could you show me different ways to use grep,(I am trying to get the first,second and first column form file,and I am counting the chars) let name=`grep "$id" product | cut -c6-20` (25 Replies)
Discussion started by: lio123
25 Replies

8. Shell Programming and Scripting

Bash:How to split one string variable in two variables?

Hello, I have a paramter $param consisting just of two literals and want to split it into two parameters, so I can combine it to a new parameter <char1><string><char2>, but the following code didn't work: tmp_PARAM_1=cut -c1 $PARAM tmp_PARAM_2=cut -c2 $PARAM... (2 Replies)
Discussion started by: ABE2202
2 Replies

9. Shell Programming and Scripting

Bash string variable manipulation

In a bash script I've set a variable that is the directory name of where an executable lives. the_dir=`dirname $which myscript` which equates to something like "/path/to/dir/bin" I need to cut that down to remove the "bin" so I now have "/path/to/dir/". This sounds easy but as a... (2 Replies)
Discussion started by: Witty
2 Replies

10. UNIX for Dummies Questions & Answers

Using GREP to extract variable following a string

Hello, I'm running calculations and I need to extract a specific number from a output file. So far I've only been able to GREP entire lines containing the string: '1 F=' . I would like to go a step further and extract just the number following '1 F='. The entire line looks like: 1 F=... (10 Replies)
Discussion started by: modey3
10 Replies
Login or Register to Ask a Question