LinuxQuestions.org
Go Job Hunting at the LQ Job Marketplace
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 07-23-2010, 08:42 PM   #1
Telengard
Member
 
Registered: Apr 2007
Location: USA
Distribution: Kubuntu 8.04
Posts: 579
Blog Entries: 8

Rep: Reputation: 147Reputation: 147
Question awk regex /f[eo]{2}t/ does not work as advertised


Please refer to this article.

Quote:
Specific Repetition

If a regular expression is to be matched a particular number of times, curly brackets ({}) can be used. For example:

/f[eo]{2}t/

This matches "foot" or "feet".
Code:
$ awk '/f[eo]{2}t/'
feet
foot
$ ls -l $( which awk )
lrwxrwxrwx 1 root root 21 2009-04-02 11:12 /usr/bin/awk -> /etc/alternatives/awk
$ ls -l /etc/alternatives/awk
lrwxrwxrwx 1 root root 13 2009-04-02 11:12 /etc/alternatives/awk -> /usr/bin/mawk
$ lsb_release -dc
Description:    Ubuntu 8.04.4 LTS
Codename:       hardy
man awk gives the the manpage for MAWK Version 1.2, and I can not find any reference to the {n} construction therein.

What am I to make of this?

Last edited by Telengard; 07-23-2010 at 08:44 PM. Reason: remove emoticon
 
Old 07-23-2010, 08:46 PM   #2
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 241Reputation: 241Reputation: 241
if you use gawk, use the --re-interval or --posix option to enable interval expressions. (ie {}).
 
Old 07-23-2010, 09:00 PM   #3
Telengard
Member
 
Registered: Apr 2007
Location: USA
Distribution: Kubuntu 8.04
Posts: 579
Blog Entries: 8

Original Poster
Rep: Reputation: 147Reputation: 147
Quote:
Originally Posted by ghostdog74 View Post
if you use gawk, use the --re-interval or --posix option to enable interval expressions. (ie {}).
Code:
$ whereis gawk
gawk:
As explained in the original post, system came with mawk. I do not have gawk.

Should I just assume that the Awk language is broken on Ubuntu?
 
Old 07-23-2010, 09:05 PM   #4
wje_lq
Member
 
Registered: Sep 2007
Location: Mariposa
Distribution: Debian lenny, Slackware 12
Posts: 809

Rep: Reputation: 178Reputation: 178
There are at least two things wrong with that article.

First, it does not recognize that there are several varieties of awk out there. You"re running Kubuntu, right? My wife runs Xubunu, and her awk is mawk, just like yours, and it behaves just like yours. There seems to be no provision therein for interval expressions, and the [KX]ubuntu man page reflects this.

On Slackware, when I ask for awk, I get GNU awk. Run in the normal manner, it doesn't have interval expressions either. But if you run it with the --posix option or the --re-interval option, interval expressions are allowed. Here's my experience with GNU awk:
Code:
evans:~$ awk '/f[eo]{2}t/'
feet
foot
evans:~$ awk --posix '/f[eo]{2}t/'
feet
feet
foot
foot
evans:~$
The second thing wrong with the article is that it seems to have a superficial understanding of regular expressions.
Code:
/f[eo]{2}t/
also matches these two lines:
Code:
feot
foet
 
1 members found this post helpful.
Old 07-23-2010, 09:12 PM   #5
wje_lq
Member
 
Registered: Sep 2007
Location: Mariposa
Distribution: Debian lenny, Slackware 12
Posts: 809

Rep: Reputation: 178Reputation: 178
getting gawk

You can install gawk on your Kubuntu system. Get the package from http://packages.debian.org/stable/gawk
 
Old 07-23-2010, 09:36 PM   #6
Telengard
Member
 
Registered: Apr 2007
Location: USA
Distribution: Kubuntu 8.04
Posts: 579
Blog Entries: 8

Original Poster
Rep: Reputation: 147Reputation: 147
Thank you for explaining it so well, wje_lq. That leads me to a conclusion and another question or two.

Quote:
Originally Posted by wje_lq View Post
First, it does not recognize that there are several varieties of awk out there. You"re running Kubuntu, right? My wife runs Xubunu, and her awk is mawk, just like yours, and it behaves just like yours.
This really irks me. It means Ubuntu users can not share Awk scripts with users of other systems. Ubuntu is one of the most popular Linux distros (maybe the most popular AFAIK), and Awk is broken on it.

What should I do when writing my own scripts to share?

Quote:
Code:
/f[eo]{2}t/
also matches these two lines:
Code:
feot
foet
That is exactly what I thought when I was reading it. "[eo]" is a complete regular expression which matches either "e" or "o", and that regular expression is repeated twice. I wonder if the authors are really so naive or if they are just trying to keep it simple for beginners.

It seems I won't be able to proceed with the tutorial any further unless I install gawk. Maybe I can figure out how to make it work with the alternatives system so that it doesn't break anything which may depend upon idiosyncrasies of mawk.

 
Old 07-23-2010, 09:44 PM   #7
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 241Reputation: 241Reputation: 241
i am really sceptical about gawk not being present. Why don't you try a find on your system to see if its really installed. the which command is not really foolproof in that it searches for $PATH only.
 
Old 07-23-2010, 10:45 PM   #8
pr_deltoid
Member
 
Registered: Jun 2010
Distribution: Fedora
Posts: 289

Rep: Reputation: 41
I was just using Kubuntu, and I know that when I ran "update-alternatives --all" awk was gawk by default. But anyways, all you have to do is install it if you don't have it. It has a higher priority and update-alternatives will set it as the default alternative automatically. If you ever have to change the alternatives, all you have to do is use "update-alternatives".

Last edited by pr_deltoid; 07-23-2010 at 10:46 PM.
 
1 members found this post helpful.
Old 07-23-2010, 11:59 PM   #9
Telengard
Member
 
Registered: Apr 2007
Location: USA
Distribution: Kubuntu 8.04
Posts: 579
Blog Entries: 8

Original Poster
Rep: Reputation: 147Reputation: 147
Quote:
Originally Posted by prdeltoid
I was just using Kubuntu, and I know that when I ran "update-alternatives --all" awk was gawk by default. But anyways, all you have to do is install it if you don't have it. It has a higher priority and update-alternatives will set it as the default alternative automatically. If you ever have to change the alternatives, all you have to do is use "update-alternatives".
Hmm. What version of Kubuntu do you have there? I have not knowingly removed gawk, nor would I.

Code:
$ aptitude search gawk
p   gawk                            - GNU awk, a pattern scanning and processing
p   gawk-doc                        - Documentation for GNU awk
Good to know! Thank you very much for the helpful info, prdeltoid.

Quote:
Originally Posted by ghostdog74 View Post
Why don't you try a find on your system to see if its really installed.
Code:
$ cd /
$ sudo updatedb
$ locate -ib gawk
/usr/share/doc/gettext-doc/examples/hello-gawk
/usr/share/locale-langpack/en_GB/LC_MESSAGES/gawk.mo
$ find bin/ lib/ opt/ sbin/ usr/ -type f -iname '*gawk*'
usr/share/locale-langpack/en_GB/LC_MESSAGES/gawk.mo
$ aptitude show gawk | grep -i state
State: not installed
Satisfied?
 
Old 07-24-2010, 12:10 AM   #10
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 241Reputation: 241Reputation: 241
Quote:
Originally Posted by Telengard View Post
Satisfied?
the question is , are you satisfied.? If you are not, then download gawk. for normal operations, gawk is just as fast.
 
Old 07-24-2010, 12:30 AM   #11
Telengard
Member
 
Registered: Apr 2007
Location: USA
Distribution: Kubuntu 8.04
Posts: 579
Blog Entries: 8

Original Poster
Rep: Reputation: 147Reputation: 147
Quote:
Originally Posted by ghostdog74 View Post
the question is , are you satisfied.?
I was merely responding to your statement that you were skeptical about gawk not being present on my system. I was already convinced that it was not, but you were probably correct to suggest I not rely completely on whereis to determine such.

As to whether I am satisfied, I have mixed feelings about this. I don't understand why my Kubuntu system shipped with a crippled Awk interpreter whereas other people's Kubuntu systems apparently did not. Maybe I should just chalk it up to Ubuntu's fickleness and add it to my list of reasons to move to Slackware.

On the other hand I can't claim that my original post has not been satisfied, so I'll probably mark this one solved for now.
 
Old 07-24-2010, 01:13 AM   #12
Telengard
Member
 
Registered: Apr 2007
Location: USA
Distribution: Kubuntu 8.04
Posts: 579
Blog Entries: 8

Original Poster
Rep: Reputation: 147Reputation: 147
Code:
$ gawk --posix '/f[eo]{2}t/'
feet
feet
foot
foot
foet
foet
feot
feot
Okay this is defintely solved now. Thank you everyone for helping.
 
Old 07-24-2010, 01:27 AM   #13
pr_deltoid
Member
 
Registered: Jun 2010
Distribution: Fedora
Posts: 289

Rep: Reputation: 41
Quote:
Hmm. What version of Kubuntu do you have there?
I was using Kubuntu 10.4 ...

EDIT:
gawk might have been installed as a dependency when I was installing something else. I installed vim, build-essential, firefox, etc. Since it had a higher priority for update-alternatives, it could've just been made the default after it was installed as a dependency.

Last edited by pr_deltoid; 07-24-2010 at 01:30 AM.
 
Old 07-24-2010, 02:36 AM   #14
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,693

Rep: Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988
I am running Ubuntu 10.04 and confirm that by default gawk is not available and mawk was the only option.

Upon getting more involved with [g]awk scripting I have since removed mawk as it had other limitations too, such as no gensub.
 
1 members found this post helpful.
Old 07-24-2010, 02:43 AM   #15
pr_deltoid
Member
 
Registered: Jun 2010
Distribution: Fedora
Posts: 289

Rep: Reputation: 41
I know that when I was going through the LFS book, it said to make sure that gawk was used, because mawk could not do the things that had to be done. I was using Debian, and it was using mawk by default.
 
1 members found this post helpful.
  


Reply

Tags
awk, gawk, regex, regular expressions


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
[SOLVED] awk/regex with a post nickvence Linux - Newbie 4 04-06-2010 01:02 AM
awk regex with variable bertl1982 Linux - General 2 03-17-2010 09:38 AM
What should some Regex match in awk? sebelk Programming 7 11-20-2009 07:38 PM
Awk regex question uncle-c Linux - Newbie 2 03-03-2009 09:30 AM
awk regex question Guest1234 Programming 6 12-25-2007 02:31 PM


All times are GMT -5. The time now is 03:41 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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration