[SOLVED] Can I use variables as patterns for grep?
by pbadillo555 from LinuxQuestions.org on (#5NPH5)
Greetings all,
I found this code of grep to search for multiple patterns on same line in any order.
Code:grep -P '(?=.*?word1)(?=.*?word2)(?=.*?word3)^.*$' fooi test it and it works as is.
Code:grep -P '(?=.*?brown)(?=.*?red)(?=.*?yellow)^.*$' sample.txt
the brown fox jumped over the red and yellow fence
the yellow bird flew over the brown and red cage
Then, I tried to use variables as patterns but it didn't work. here is my script.
Code:#!/bin/bash
a=brown
b=red
c=yellow
grep -P '(?=.*?"$a")(?=.*?"$b")(?=.*?"$c")^.*$' sample.txtWhen I run it, no results appear.
Can grep use vaiables as patterns?
Thanks
I found this code of grep to search for multiple patterns on same line in any order.
Code:grep -P '(?=.*?word1)(?=.*?word2)(?=.*?word3)^.*$' fooi test it and it works as is.
Code:grep -P '(?=.*?brown)(?=.*?red)(?=.*?yellow)^.*$' sample.txt
the brown fox jumped over the red and yellow fence
the yellow bird flew over the brown and red cage
Then, I tried to use variables as patterns but it didn't work. here is my script.
Code:#!/bin/bash
a=brown
b=red
c=yellow
grep -P '(?=.*?"$a")(?=.*?"$b")(?=.*?"$c")^.*$' sample.txtWhen I run it, no results appear.
Can grep use vaiables as patterns?
Thanks