LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
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, 02: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, 02:38 PM   #2
osor
HCL Maintainer
 
Registered: Jan 2006
Distribution: (H)LFS, Gentoo
Posts: 2,450

Rep: Reputation: 78
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-12-2007, 11:31 PM   #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



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

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 02:54 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
Open Source Consulting | Domain Registration