LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
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 11-10-2010, 12:49 PM   #1
macsdev
LQ Newbie
 
Registered: Nov 2010
Posts: 3

Rep: Reputation: 0
grep searching for strings with '(apostrophe)


I am trying a search for a pattern in the file. I can have any character in the pattern. I am pretty sure I will have $, ", ', ^, ` etc., The Problem I am facing is if I use "" (double quotes) to enclose the pattern, it gives special meaning to $, ^ and " within the string. I have no control over the pattern input. I am getting it from some other file. On the other hand, If I use '' (single quotes) to enclose the pattern, it gives special meaning to the ' (apostrophe) within the string and terminates the pattern prematurely. How do I disable the special meaning these characters have? For example, in perl, I could enclose the pattern within \Q and \E. Is there an equivalent in grep pattern expression? I could find one in the man page of grep. Is there a solution to this problem?

Thanks.
 
Old 11-10-2010, 12:54 PM   #2
GrapefruiTgirl
LQ Guru
 
Registered: Dec 2006
Location: underground
Distribution: Slackware64
Posts: 7,594

Rep: Reputation: 556Reputation: 556Reputation: 556Reputation: 556Reputation: 556Reputation: 556
I'm sure there's a solution, but as you see, exactly how it works depends on exactly what characters you're having trouble with, what sort of regex you're using (regular, extended, PCRE, etc..) and which chars you want to match, and which tool (grep in this case).

The most sure way to get some helpful advice, is to show us a sample of the real input file(s) and data you're grepping. That way someone can examine what regex and special chars you're dealing with and do hands on testing. If you have some code you're currently working on (grep commands, a script, etc..), please show us that too.
 
Old 11-10-2010, 01:00 PM   #3
macsdev
LQ Newbie
 
Registered: Nov 2010
Posts: 3

Original Poster
Rep: Reputation: 0
The Code which I am using is as follows:

Code:
grep -c 'pattern1.*pattern2' file.txt
Usually pattern2 contains various special characters. For Instance, pattern 2 could be the following string:

'1000s feared dead from earthquake in Haiti.
 
Old 11-11-2010, 06:53 AM   #4
neonsignal
Senior Member
 
Registered: Jan 2005
Location: Melbourne, Australia
Distribution: Debian Bookworm (Fluxbox WM)
Posts: 1,391
Blog Entries: 54

Rep: Reputation: 360Reputation: 360Reputation: 360Reputation: 360
When using double quotes, you can use a backslash to escape characters within the string. For example:
Code:
echo "\$PATH-\`whoami\`-\"-\\"
will echo:
Code:
$PATH-`whoami`-"-\
When using single quotes, you can use a backslash to escape a single quote outside of the string. For example:
Code:
echo 'chang'\''eng'
will echo
Code:
chang'eng
(a concatenation, starting with the quoted first part of the string, followed by an escaped single quote, followed by the quoted second part of the string)

Notice that this is a bash issue, not a grep one. Once the string is passed to grep, you will still have to contend with the meanings that grep assigns to various special characters (such as the dollar sign).
 
Old 11-11-2010, 10:38 PM   #5
macsdev
LQ Newbie
 
Registered: Nov 2010
Posts: 3

Original Poster
Rep: Reputation: 0
My problem is that the patterns come from another file. It's impossible for me check for all the meta characters present in the pattern and escape each and every meta character individually. Is there a work around? What I want is to somehow make grep understand that any meta characters present are not actually meta but part of the pattern.
 
Old 11-11-2010, 11:46 PM   #6
neonsignal
Senior Member
 
Registered: Jan 2005
Location: Melbourne, Australia
Distribution: Debian Bookworm (Fluxbox WM)
Posts: 1,391
Blog Entries: 54

Rep: Reputation: 360Reputation: 360Reputation: 360Reputation: 360
Quote:
Originally Posted by macsdev View Post
It's impossible for me check for all the meta characters present in the pattern and escape each and every meta character individually. Is there a work around? What I want is to somehow make grep understand that any meta characters present are not actually meta but part of the pattern.
Nothing is impossible; for example, you could have your script insert the escape characters in the patterns.

You can use 'grep -F' (equivalent to fgrep) to ignore all meta characters.

But I suspect you don't want to ignore all meta characters. You want to ignore some but not others. So if perl regular expression syntax does what you want, why not use it instead of grep? It can be still be done as a single command line ('perl -pe').
 
  


Reply



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
[SOLVED] Searching and replacing strings in a file with strings in other files xndd Linux - Newbie 16 07-29-2010 02:40 PM
Bash - Searching strings for array elements... Phier Programming 18 05-09-2010 04:37 AM
grep two strings Hondro Linux - General 3 09-08-2008 09:55 PM
Searching through strings in StarBasic/OpenOffice BASIC scuffell Programming 1 03-31-2006 09:50 AM
Searching files for strings tmoorman Linux - Software 4 01-08-2004 01:46 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:55 PM.

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
Open Source Consulting | Domain Registration