LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
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 09-04-2011, 01:40 AM   #1
915086731
Member
 
Registered: Apr 2010
Posts: 144
Blog Entries: 6

Rep: Reputation: 2
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 ] .
 
Old 09-04-2011, 01:53 AM   #2
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
Hi,

The [ and ] are special and you do need to escape them (I.e. use backslashes).

This will search for [Aa] and not an A or an a:
Code:
awk '/\[Aa\]/ { print }' infile
GNU Awk - 3.3 Regular Expression Operators

Hope this helps.
 
Old 09-04-2011, 06:56 AM   #3
915086731
Member
 
Registered: Apr 2010
Posts: 144

Original Poster
Blog Entries: 6

Rep: Reputation: 2
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?

Last edited by 915086731; 09-04-2011 at 06:58 AM.
 
Old 09-04-2011, 07:16 AM   #4
kurumi
Member
 
Registered: Apr 2010
Posts: 228

Rep: Reputation: 53
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
 
Old 09-04-2011, 07:29 AM   #5
915086731
Member
 
Registered: Apr 2010
Posts: 144

Original Poster
Blog Entries: 6

Rep: Reputation: 2
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?
 
Old 09-04-2011, 10:09 AM   #6
rknichols
Senior Member
 
Registered: Aug 2009
Distribution: Rocky Linux
Posts: 4,779

Rep: Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212
In awk, the index() function performs exact, non-regex, matches.
Code:
awk 'index($0, "some_string") != 0 { print }'
 
1 members found this post helpful.
Old 09-04-2011, 10:46 AM   #7
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 10,659
Blog Entries: 4

Rep: Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941
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.
 
Old 09-07-2011, 03:02 PM   #8
XavierP
Moderator
 
Registered: Nov 2002
Location: Kent, England
Distribution: Debian Testing
Posts: 19,192
Blog Entries: 4

Rep: Reputation: 475Reputation: 475Reputation: 475Reputation: 475Reputation: 475
Moved to Programming
 
Old 09-07-2011, 10:07 PM   #9
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

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


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
Awk - substitution with parts of the matched regular expression used. c_moriarty Programming 4 04-30-2011 08:38 AM
[SOLVED] AWK: compare apache dates without using regular expression smallmeans Programming 13 05-17-2010 02:36 AM
Help with Regular Expression in VIM search and replace vijay_babu1981 Linux - Newbie 8 10-22-2009 07:49 AM
Regular expression to match a valid URL string vharishankar Programming 13 07-21-2005 09:17 PM
regular expression search/replace question (HELP!!! :) amytys Programming 5 09-06-2004 02:36 PM

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

All times are GMT -5. The time now is 06:02 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