How can awk search a string without using regular expression?
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.
How can awk search a string without using regular expression?
Hello,
Awk seem treat the pattern as regular expression, how can awk search not using regular expression? e.g. [Aa] just represent for "[Aa]", not "A" or "a" . I don't want to add backslash [ and ] .
Thanks , I know to add backslash before special symbol, but I do not want to use so complex way. Does awk support a option to ignore these special symbol, and do not use regular expression?
e.g. I get a string from arguments, then search it in a file, the string may contain these special symbol, but add backslash before these special symbol is very complex, how to resolve it?
You can of course use substitution to change those metacharacters to prepend with slash. However, if you can use other programming language besides awk, here's one in Ruby
Code:
$ ruby -ne 'print if $_["[Aa]"]' file # [Aa] is the literal string you want to search.
if you want to pass it from the shell
Code:
$ string="[Aa]"
$ ruby -ne 'print if $_["'$string'"]' file
Thanks , I do want to use other languages. Do I must add backslash before the special symbols by using awk ? How to write a shell function to auto add backslash before the special symbol?
915..., I cordially suggest to you that "the answer is, yes." Add the backslashes.
Awk is a well-built tool that is purpose-built for string matching and text-file manipulation. Regular expressions are one of its core strengths. Use those core strengths. There are compelling reasons why awk is built to work as it does, and there are many subtle strengths to reward your very diligent study of this tool.
You can also include a variable reference into a regular-expression, in which case awk is looking for "whatever this variable currently contains." (Set the variable's value to "[ab]".)
Notice that in this case the regular-expression is compiled to make a reference to the current content of the variable, so that the variable's current value is referenced each time the regular expression is evaluated, not when the regular-expression is compiled prior to its first use. (It is also possible to specify the pattern using a variable, i.e. so that the regular-expression definition is obtained from a variable's value, but that's another story for another day.)
Google this search-string, being mindful of the quotes: awk "regular expression" variable
Last edited by sundialsvcs; 09-04-2011 at 10:51 AM.
I would be curious how you are using the string? Placing it in quotes opposite ~ or !~ is when it is used as
a computed regex (as described here), but when used opposite say == it will be just a string.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.