LinuxQuestions.org
Help answer threads with 0 replies.
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-19-2011, 10:00 AM   #16
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037

I recall reading somewhere that when you use a variable in a test, the contents of it are treated as a regex, meaning you should store the entire pattern in the variable and leave the /../ brackets off. I can't locate any clear statement for it in the gawk manual, but it does appear to be true in testing:

Code:
$ x=78

$ awk -v y="^$x" '$0 ~ y { print }' file.txt
78 foo

$ awk -v y="^$x" 'match( $0 , y ) { print }' file.txt
78 foo
Note though that variables can't be used alone as matching patterns, you have to use a full syntax expression of some kind. This fails to match anything:

Code:
$ awk -v y="^$x" 'y { print }' file.txt
Edit: When using \y and similar operators in these patterns, you'll have an added problem of the shell condensing backslashes. You need to use three backslashes inside double quotes to ensure that one will remain in the final regex.
Code:
$ awk -v y="\\\y$x\\\y" '$0 ~ y {print}' file.txt
78 foo
100 foo 78
50 foo 78 bar
Edit2: I just noticed the comment mentioning using mawk. Unfortunately it appears that mawk doesn't support the \y flag. It looks ugly, but how about a regex that tests for the value if at the beginning or end, or if surrounded by spaces? This works with mawk on my machine:
Code:
$ mawk -v y="(^$x|$x$|[ \t]$x[ \t])" '$0 ~ y { print }' file.txt
78 foo
100 foo 78
50 foo 78 bar

Last edited by David the H.; 09-19-2011 at 10:35 AM. Reason: added
 
Old 09-19-2011, 10:13 AM   #17
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
Ok. This is a mawk problem, since word boundaries are a GNU awk (gawk) extension. You can try something different, like:
Code:
awk '/[^0-9]'$x'[^0-9]/' FILE
This will exclude any pattern with digits before and digits after $x. You can extend the character list to exclude also alphabetic characters a-z and A-Z. This can be accomplished more concisely using the [:alnum:] character class, e.g.
Code:
awk '/[^[:alnum:]]'$x'[^[:alnum:]]/' FILE

Last edited by colucix; 09-19-2011 at 10:19 AM.
 
Old 09-19-2011, 11:18 AM   #18
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,005

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
Well the first suggestion I would have is to remove mawk and install gawk. It sounds harsh but mawk has a number of limitations that i have run into (not really sure why it was created actually).

All of colucix's suggestions are good and can be used with a variable and computed regex as well (at least I hope so)
 
Old 09-19-2011, 11:28 AM   #19
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
I'm not getting any output with character classes in mawk. The same patterns do work in gawk and nawk, so it looks like mawk just doesn't support them either.
 
Old 09-19-2011, 12:16 PM   #20
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
Quote:
Originally Posted by David the H. View Post
I'm not getting any output with character classes in mawk. The same patterns do work in gawk and nawk, so it looks like mawk just doesn't support them either.
It works for me, either with mawk 1.3.4 both on OpenSuSE and CentOS. I cannot test on Ubuntu, anyway.
Code:
$ mawk '/[^[:alnum:]]'$x'[^[:alnum:]]/' testfile
  tP       75 P4   76 P4 1    77 P4 2    78 P4 3    89 P422   90 P42 1 2
 
Old 09-19-2011, 01:31 PM   #21
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
Hmm. Version listed as 1.3.3 here. The exact same command and data isn't giving me anything. Again, nawk and gawk do just fine.

The release page does say that some unspecified "improvements" to the regex engine were added recently.

http://freshmeat.net/projects/mawk/releases

Debian's always behind the times.
 
  


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] Match datetime by the minute (not an exact match by the second) [mysql] hattori.hanzo Programming 1 10-21-2010 05:43 PM
egrep for only words(exact match) msgforsunil Linux - Newbie 4 04-14-2010 05:27 AM
Awk: match string in exact position sebelk Programming 2 10-19-2009 02:15 PM
grep/sed/awk - find match, then match on next line gctaylor1 Programming 3 07-11-2007 08:55 AM
how to find an exact substring match? ldp Programming 7 02-22-2005 06:28 AM

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

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