Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I am having trouble understanding or explaining why the following expression fails in gawk 3.1.x and yet works in gawk 4.1.x:

(Minimum working example)

echo ";#ADCDE#" | awk '/#.{5}#$/' -> produces a match in gawk 4.1.x, does not produce a match in gawk 3.1.x

echo ";#ADCDE#" | awk '/#.*#$/' -> produces a match in both

Did something change between gawk 3 and 4 in how regular expressions are handled? I didn't think the repetition construct {n} was new for regular expressions. The same behaviour happens if I change the dot (.) with a character class or [A-Z]

share|improve this question
add comment

1 Answer

up vote 4 down vote accepted

Adding --posix works in 3.1

echo ";#ADCDE#" | gawk --posix '/#.{5}#$/'

I have

awk --version
GNU Awk 3.1.6
...

From my man awk page

   r{n}
   r{n,}
   r{n,m}     One  or two numbers inside braces denote an interval expres‐
              sion.  If there is one number in the braces,  the  preceding
              regular  expression r is repeated n times.  If there are two
              numbers separated by a comma, r is repeated n  to  m  times.
              If  there  is  one  number  followed  by  a comma, then r is
              repeated at least n times.
              Interval expressions are only available if either --posix or
              --re-interval is specified on the command line.
share|improve this answer
    
Indeed works for me as well. The odd part is you don't need the --posix switch for gawk 4 but the man page reads the same. Thoughts? –  user153222 Feb 24 at 20:29
    
and the man page states that intervals are only available with --posix, but my example is not an interval.... is this a gawk bug? –  user153222 Feb 24 at 20:37
1  
I haven't been following discussions regarding this, but it is probably mentioned in the Changelog for the library which does the regex matching. I noticed earlier in the man page, that it mentions it follows egreps regex's ... –  X Tian Feb 24 at 20:37
1  
@user153222 It says "one or two numbers inside {} denote an interval". –  X Tian Feb 24 at 20:40
1  
I found this comment in the gawk 4.0.0 changelog: 8. Interval expressions are now part of default regular expressions for GNU Awk syntax. link I guess that settles it. –  user153222 Feb 24 at 21:07
show 2 more comments

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.