LinuxQuestions.org
View the Most Wanted LQ Wiki articles.
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 12-22-2007, 02:04 PM   #1
Guest1234
Member
 
Registered: May 2004
Distribution: Ubuntu
Posts: 33

Rep: Reputation: 15
awk regex question


I know how to use regular expressions in awk for checking if a line
matches a certain pattern but I don't know how to extract groups from the pattern.

for example if I have a line of text containing the current date:
"22-12-2007"
in perl I could do something like this:
$line ~= /(\d{2})-(\d{2})-(\d{4})/;
And extract what inside the brackets simply by:
$day = $1;
$month = $2;
$year = $3;

How can I extract the inside of a bracket in a regular expression in awk?

Thanks
 
Old 12-22-2007, 02:28 PM   #2
Uncle_Theodore
Member
 
Registered: Dec 2007
Location: Charleston WV, USA
Distribution: Slackware 12.2, Arch Linux Amd64
Posts: 896

Rep: Reputation: 60
Is this what you want?

teddy@toshiba~$ echo "22-12-2007" | awk '{ fields=split($1, entries, "-"); print entries[1]; print entries[2]; print entries[3] }'
22
12
2007
teddy@toshiba~$

It's the split function in awk that does the job.
 
Old 12-22-2007, 09:04 PM   #3
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 241Reputation: 241Reputation: 241
if your string variable originated in the shell, eg in bash
Code:
# string="22-12-2007"
# IFS="-"
# set -- $($string)
# echo $1 $2 $3
22 12 2007
# IFS=" "
# echo $string | awk -F"-" '{print $1,$2,$3}'   
22 12 2007
if the variable is generated inside awk
Code:
awk 'BEGIN {}
...
n=split(string,mystring,"-")
print mystring[1],mystring[2],mystring[3]
...
END {} ' file
 
Old 12-23-2007, 02:01 PM   #4
Guest1234
Member
 
Registered: May 2004
Distribution: Ubuntu
Posts: 33

Original Poster
Rep: Reputation: 15
Thanks for the replies

However I'm looking for something more generic, the date extraction was just an example.

Is it possible to extract groups(the thing inside the brackets) with regular expressions in awk, and if so how do you that?
 
Old 12-23-2007, 05:43 PM   #5
muha
Member
 
Registered: Nov 2005
Distribution: xubuntu, grml
Posts: 451

Rep: Reputation: 37
Sed is not an option?
Code:
$ echo "dfgsfgfgdsfgd\"23-12-2007\"sdfgsdfgsafdaq" | sed -n 's/.*"\(.*\)".*/\1/p' -
23-12-2007
edit: A field separator in awk might be an option?
Code:
$ cat text.txt 
adsdasdasdasd"22-12-2007"asdasdasdasdasdadas
dfgsfgfgdsfgd"23-12-2007"sdfgsdfgsafdaq
$ awk -F'"' '{print $1}' text.txt 
adsdasdasdasd
dfgsfgfgdsfgd
$ awk -F'"' '{print $2}' text.txt 
22-12-2007
23-12-2007
$ awk -F'"' '{print $3}' text.txt 
asdasdasdasdasdadas
sdfgsdfgsafdaq

Last edited by muha; 12-23-2007 at 05:49 PM.
 
Old 12-23-2007, 09:05 PM   #6
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 241Reputation: 241Reputation: 241
Quote:
Originally Posted by Guest1234 View Post
Is it possible to extract groups(the thing inside the brackets) with regular expressions in awk, and if so how do you that?
yes, with GNU awk gensub(). see here
 
Old 12-25-2007, 02:31 PM   #7
Guest1234
Member
 
Registered: May 2004
Distribution: Ubuntu
Posts: 33

Original Poster
Rep: Reputation: 15
Thanks guys, you helped a lot
 
  


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
Awk scripting and usage of regex to locate a hyperlink grinch Programming 12 04-30-2007 04:50 PM
Newbie SED / AWK / Regex command help request Critcho Linux - Newbie 10 03-19-2007 12:22 PM
Regex question once again Isotonik Linux - Newbie 2 06-14-2006 03:15 PM
regex question Toadman Linux - General 0 12-30-2005 01:59 PM
sed / regex question whysyn Linux - General 3 06-28-2005 03:11 PM


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