LinuxQuestions.org
Share your knowledge at the LQ Wiki.
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 11-19-2009, 06:54 AM   #1
sebelk
Member
 
Registered: Jan 2007
Posts: 66

Rep: Reputation: 15
What should some Regex match in awk?


Hi,

What should the following expression match? Could you explain me?

([^[(<]])


Thanks in advance!
 
Old 11-19-2009, 07:58 AM   #2
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682
This smells a bit like a homework question. I'll give a small hint. You can remove the outer `(' and `)' since they are for grouping. Now try to explain the rest.

There is a regex manpage. man 7 regex. Read through that.
 
Old 11-19-2009, 08:01 AM   #3
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
What is it **supposed** to do? Please give us some context for the question.

I tried various combinations and stared at ABS* for 15 minutes with no success.

Examples:

[^a] matches any character which is not "a"
[^ab] matches any character which is not "a" or "b"
[^[ab]] OR [[ab]] does not appear to match anything. But neither produces an error message. (It does not match--eg-- the literal set of characters "[", "a", "b", or "]")

I normally associate nested brackets ([[...]]) with character classes, as in "[[:alpha:]]" (meaning any letter)
OR
[^[:alpha]] meaning anything which is not a letter. So it could be looking for a character class, but then I would have expected an error message.

*Advanced BASH Scripting Guide
 
Old 11-19-2009, 08:07 AM   #4
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
Quote:
Originally Posted by jschiwal View Post
This smells a bit like a homework question. I'll give a small hint. You can remove the outer `(' and `)' since they are for grouping. Now try to explain the rest.

There is a regex manpage. man 7 regex. Read through that.
I thought homework also, but the posting history says maybe not.

If it is homework, how about a trick question? My favorite--in chemistry class: What is this compound? BaNa2 (subscript 2)
 
Old 11-19-2009, 09:02 AM   #5
sebelk
Member
 
Registered: Jan 2007
Posts: 66

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by pixellany View Post
I thought homework also, but the posting history says maybe not.

If it is homework, how about a trick question? My favorite--in chemistry class: What is this compound? BaNa2 (subscript 2)
Believe that I've read man pages but parentheses and angle brackets are tricky characters

There is no tricks or homework. The only trick here is that It's my fault for I haven't documented my own scripts that has the function that I use for parsing radius logs:

Code:
Incorrect(){
#I don't want to match with some other kind of Incorrect Access
awk '$9 ~ /incorrect/  && $10 ~ /([^[(<]])/ { print tolower($10) }'
}
Really parentheses are not needed here. So we have:

[^[(<]]

So this match on whatever be on 10th field that has parentheses or angle brackets...

I prevent so that entries like:

[<no
(rlm_ldap:


are included int the output

I was confused because I was playing with:

Code:
awk '$9 ~ /incorrect/  && $10 ~ /([[(<]])/ { print tolower($10) }' logfile

and it outputs nothing, but even so the expression above works fine,

So I've just remembered and I am documenting what I wanted to do :

Let's suppose we have a file "q" with:


[<xxxxx ]
(
[saralnadasd]

We want match only '[saralnadasd]'

If I use

Code:
awk '$1  ~  /[^(<]/ {print $1}' q
I get:

[<xxx
(xxxx
[saralnadasd]

that is not what we are looking for.

If I use
Code:
awk '$1  ~  /^[(<]/ {print $1}' q
(xxxx

Of course because "^" out of squared brackets means beginning of the line. That is not we want again

It works if we use:

Code:
awk '$1  ~  /[^[(<]]/ {print $1}' q
[saralnadasd]


I was confused because removing "^" from the regex is not the right way to get the reverse thing (yes, yes I know that I can use "!~ in awk, but was trying to remember what I did), the correct reverse regex is:

Code:
 awk '$1  ~  /[(<]/ {print $1}' q

[<xxx
(xxxx


I'd hope that my somewhat confuse explanation be useful to someone
 
Old 11-19-2009, 09:04 AM   #6
GrapefruiTgirl
LQ Guru
 
Registered: Dec 2006
Location: underground
Distribution: Slackware64
Posts: 7,594

Rep: Reputation: 556Reputation: 556Reputation: 556Reputation: 556Reputation: 556Reputation: 556
Quote:
Originally Posted by pixellany View Post
If it is homework, how about a trick question? My favorite--in chemistry class: What is this compound? BaNa2 (subscript 2)
Hmm.. I had to but isn't that the chemical formula for synthetic bananas (patented by MS to feed those millions of monkeys we discussed earlier)?
 
Old 11-19-2009, 01:18 PM   #7
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
Quote:
Originally Posted by sebelk View Post
I'd hope that my somewhat confuse explanation be useful to someone
Sorry, but you lost me. I have the impression that you now know what that REGEX does. If so, it there any way to describe it in just a few lines?
 
Old 11-20-2009, 06:38 PM   #8
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682
Code:
([^[(<]])
The "^" is the negate operator.
[^[(<] -- This matches any character that isn't a "[", "(" or "<"

Here is the part that may trip you up:
The last square bracket is another character.

So It matches .] where . isn't in the set of [ "[", "(", "<" ].

In the string abcd], it will match "d]".
Code:
$ echo abcd] | sed -r 's/([^[(<]])/:\1:/'
abc:d]:
 
  


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
Help with regex to match an IP abefroman Linux - Server 5 11-02-2009 03:07 PM
Extract everything before a regex match onesikgypo Programming 5 10-21-2009 04:49 AM
Perl Regex multiline match issues adymcc Linux - General 2 03-31-2008 09:45 AM
grep/sed/awk - find match, then match on next line gctaylor1 Programming 3 07-11-2007 08:55 AM
help me match this regex line (easy) JustinHoMi Programming 7 03-17-2002 01:43 AM

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

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