Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Complex RegEx in AWK errors on execution

Status
Not open for further replies.

bdmeyer

IS-IT--Management
Feb 27, 2005
5
US
This is working from the command line:
grep -P --color '\b(?!000)(?!666)(?:[0-6]\d{2}|7(?:[0-356]\d|7[012]))[- ](?!00)\d{2}[- ](?!0000)\d{4}\b' *.txt

The --color shows me it is matching exactly.

What I really need though is to not have the entire line returned. I only need the exact matches returned from inside each line. (like what color is showing me)

So I tried:

awk '/\b(?!000)(?!666)(?:[0-6]\d{2}|7(?:[0-356]\d|7[012]))[- ](?!00)\d{2}[- ](?!0000)\d{4}\b/ { print $0 }' file.text

and I get this error:

awk: illegal primary in regular expression[/color red] \b(?!000)(?!666)(?:[0-6]\d{2}|7(?:[0-356]\d|7[012]))[- ](?!00)\d{2}[- ](?!0000)\d{4}\b at[/color red] !000)(?!666)(?:[0-6]\d{2}|7(?:[0-356]\d|7[012]))[- ](?!00)\d{2}[- ](?!0000)\d{4}\b
source line number 1
context is[/color red]
/\b(?!000)(?!666)(?:[0-6]\d{2}|7(?:[0-356]\d|7[012]))[- ](?!00)\d{2}[- >>> ](?!0000)\d{4}\b/ <<< [/color red]


I believe it ha something to do with either the backtics, or more likely the '!' in the regex.

I am not hung up on using awk, I would be happy to use any tool that might parse just the matched values instead of the entire matched line, so I can dump that to a file.

Any Help, is appreciated:
--Bruce D. Meyer

 
Hi

[tt]\b[/tt] in [tt]awk[/tt] is a backspace character.

In [tt]gawk[/tt] the start of the word is [tt]\<[/tt] . As far as I know, other [tt]awk[/tt] implementations can not match word boundaries.

But while you are using [tt]grep[/tt] with the -P ( --perl-regexp ) switch, would be better to move to [tt]perl[/tt] instead. Then your regular expression should work :
Code:
perl -nae 'print $F[0] if /\b(?!000)(?!666)(?:[0-6]\d{2}|7(?:[0-356]\d|7[012]))[- ](?!00)\d{2}[- ](?!0000)\d{4}\b/' file.text
Sorry for the off-topic suggestion.

Feherke.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top