ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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?
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.
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).
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.
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').
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.