LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   using grep when the pattern contains a ! (https://www.linuxquestions.org/questions/programming-9/using-grep-when-the-pattern-contains-a-301982/)

farmerjoe 03-15-2005 02:45 PM

using grep when the pattern contains a !
 
I am try to search for a pattern using grep. The problem is that the pattern sometimes contains a !. This seems to mess grep up and call the last command entered into the console. How can I get it to stop doing this? It is important that I have the ! in the search. Please help!

Thanks,
farmerjoe

TheLinuxDuck 03-15-2005 02:51 PM

If the search phrase is a static string, try simple escaping the excl point, as:

Code:

find . -type f -exec grep -H "\!" '{}' ';'
This works.

If it's a variable string, you can do variable replacement using a replacement expression.

Matir 03-15-2005 02:53 PM

And if you're not using find, it's basically the same, but this may make things clearer:
[code]
grep "\!regexp" files

farmerjoe 03-15-2005 02:55 PM

Ah! stupid me! i thought i had tried that earlier but i guess i didnt do it correctly! Thanks for the help guys!

-farmerjoe

farmerjoe 03-15-2005 03:00 PM

Hmm..heres another question:

Before i run the grep command. I am going to need to convert my expression to one that will work if it has ! in it. I have tried echo "$EXPRESSION" | sed -e 's/!/\\!/g'
but that doesnt work because echo has the same problem that grep did. Any tips?

-farmerjoe

TheLinuxDuck 03-15-2005 03:06 PM

Code:

#!/bin/bash

exclsearch="This ! contains ! excl points!"
echo "pre: $exclsearch"
newES=${exclsearch//\!/\\\!}
echo "post: $newES"

Code:

~> ./q.sh
pre: This ! contains ! excl points!
post: This \! contains \! excl points\!


farmerjoe 03-15-2005 03:27 PM

ahhhh. i see. thanks! didnt know i could do that!

-farmerjoe

farmerjoe 03-15-2005 07:56 PM

HMm.. How would i do this with a ' ?
I've tried this but it doesnt seem to work:
START="This is'nt a test!"
PRE=${START//\!/\\\!}
echo "pre: $PRE"
POST=${PRE/\'/\\'}
echo "post: $POST"

farmerjoe 03-15-2005 08:00 PM

Ooops! figured it out!
-farmerjoe

dustu76 03-15-2005 11:04 PM

Quote:

Originally posted by farmerjoe
Hmm..heres another question:

Before i run the grep command. I am going to need to convert my expression to one that will work if it has ! in it. I have tried echo "$EXPRESSION" | sed -e 's/!/\\!/g'
but that doesnt work because echo has the same problem that grep did. Any tips?

-farmerjoe

I didn't encounter any such problems:

Code:

SF1B : /supmis/soumen/tmp > cat excl.sh
exp="This ! contains ! excl points!"
echo $exp |sed -e 's#\!#\\\!#g'
SF1B : /supmis/soumen/tmp > bash excl.sh
This \! contains \! excl points\!
SF1B : /supmis/soumen/tmp > ksh excl.sh
This \! contains \! excl points\!
SF1B : /supmis/soumen/tmp > sh excl.sh
This \! contains \! excl points\!
SF1B : /supmis/soumen/tmp >

Of course, if your shell permits, a built-in such as one suggested by TheLinuxDuck is faster.

HTH.


All times are GMT -5. The time now is 10:20 PM.