LinuxQuestions.org
View the Most Wanted LQ Wiki articles.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 03-15-2005, 03:45 PM   #1
farmerjoe
Member
 
Registered: Oct 2004
Location: Texas
Distribution: Ubuntu - Home, RHEL4 - Server
Posts: 96

Rep: Reputation: 15
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
 
Old 03-15-2005, 03:51 PM   #2
TheLinuxDuck
Member
 
Registered: Sep 2002
Location: Tulsa, OK
Distribution: Slack, baby!
Posts: 349

Rep: Reputation: 33
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.
 
Old 03-15-2005, 03:53 PM   #3
Matir
Moderator
 
Registered: Nov 2004
Location: San Jose, CA
Distribution: Ubuntu
Posts: 8,507

Rep: Reputation: 118Reputation: 118
And if you're not using find, it's basically the same, but this may make things clearer:
[code]
grep "\!regexp" files
 
Old 03-15-2005, 03:55 PM   #4
farmerjoe
Member
 
Registered: Oct 2004
Location: Texas
Distribution: Ubuntu - Home, RHEL4 - Server
Posts: 96

Original Poster
Rep: Reputation: 15
Ah! stupid me! i thought i had tried that earlier but i guess i didnt do it correctly! Thanks for the help guys!

-farmerjoe
 
Old 03-15-2005, 04:00 PM   #5
farmerjoe
Member
 
Registered: Oct 2004
Location: Texas
Distribution: Ubuntu - Home, RHEL4 - Server
Posts: 96

Original Poster
Rep: Reputation: 15
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
 
Old 03-15-2005, 04:06 PM   #6
TheLinuxDuck
Member
 
Registered: Sep 2002
Location: Tulsa, OK
Distribution: Slack, baby!
Posts: 349

Rep: Reputation: 33
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\!
 
Old 03-15-2005, 04:27 PM   #7
farmerjoe
Member
 
Registered: Oct 2004
Location: Texas
Distribution: Ubuntu - Home, RHEL4 - Server
Posts: 96

Original Poster
Rep: Reputation: 15
ahhhh. i see. thanks! didnt know i could do that!

-farmerjoe
 
Old 03-15-2005, 08:56 PM   #8
farmerjoe
Member
 
Registered: Oct 2004
Location: Texas
Distribution: Ubuntu - Home, RHEL4 - Server
Posts: 96

Original Poster
Rep: Reputation: 15
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"
 
Old 03-15-2005, 09:00 PM   #9
farmerjoe
Member
 
Registered: Oct 2004
Location: Texas
Distribution: Ubuntu - Home, RHEL4 - Server
Posts: 96

Original Poster
Rep: Reputation: 15
Ooops! figured it out!
-farmerjoe
 
Old 03-16-2005, 12:04 AM   #10
dustu76
Member
 
Registered: Sep 2004
Distribution: OpenSuSe
Posts: 153

Rep: Reputation: 30
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.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
printing pattern match and not whole line that matches pattern Avatar33 Programming 13 05-06-2009 07:17 AM
grep ?? can grep us variables? DaFrEQ Linux - Software 4 09-14-2005 01:22 PM
Grep pattern first line of a file ericcarlson Linux - Newbie 11 07-20-2004 11:51 AM
"Undeleting" data using grep, but get "grep: memory exhausted" error SammyK Linux - Software 2 03-13-2004 04:11 PM
ps -ef|grep -v root|grep apache<<result maelstrombob Linux - Newbie 1 09-24-2003 12:38 PM


All times are GMT -5. The time now is 06:53 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration