LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices

Reply
 
Search this Thread
Old 12-12-2007, 03:53 AM   #1
stalin.varanasi
LQ Newbie
 
Registered: Dec 2007
Posts: 7

Rep: Reputation: 0
RE in commands like match() inside awk.


Hello,
I want to know what should I give in the regular expression inside match command in awk,to recognise a line having two patterns in order.

i.e for example,
My source file contains the following line:

"text text text pattern1 text text text pattern2
text text text text text text text pattern2"

i want recognise the 1st line only.

awk '{if(match($0,PATTERN1*PATTERN2))print $0}' source file

the above command is matching both.

Can any body tell me what should Regular expression should I put.

-Bourne.
 
Old 12-12-2007, 03:38 PM   #2
osor
HCL Maintainer
 
Registered: Jan 2006
Distribution: (H)LFS, Gentoo
Posts: 2,450

Rep: Reputation: 70
The regular expressions (at least they way you are using them) should be constants. Also, the use of the asterisk means you want to match zero or more of the preceding token (which happens to be a 1). This is most likely not what you had in mind. Additionally, the match() function is a GNU extension used for some more powerful constructs such as placing matches in an array. There is no reason to use it for something so simple as an “if” statement.

Anyway, here is the “corrected” version using gawk’s match():
Code:
awk '{if(match($0,/PATTERN1.*PATTERN2/))print $0}' file
And here is a much more elegant version (and a style you will most likely continue to see):
Code:
awk '/PATTERN1.*PATTERN2/{print $0}' file
 
Old 12-13-2007, 12:31 AM   #3
stalin.varanasi
LQ Newbie
 
Registered: Dec 2007
Posts: 7

Original Poster
Rep: Reputation: 0
Hi OSR,
Thank you so much for your kind reply.Its working.
My main intention is to know how to write the regular expression only.But in my subject,I have put "RE in commands like match()" because I don't know whether regular expressions in ACTION part of gawk command are same as regular expressions in CONDITION part of gawk command or not.
Thanks once again.Your answer is very helpful to me.
-Stalin.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
Sed/Awk: print lines between n'th and (n+1)'th match of "foo" xaverius Programming 17 08-20-2007 12:39 PM
grep/sed/awk - find match, then match on next line gctaylor1 Programming 3 07-11-2007 09:55 AM
How to acess Variable defined in perl script inside an awk call sumin Programming 3 04-26-2007 06:19 AM
awk inside a makefile linux.fob Programming 2 10-12-2005 05:57 PM
Can I use grep inside awk? Helene Programming 6 04-27-2004 01:58 AM


All times are GMT -5. The time now is 09:40 AM.

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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration