5
 echo xx y11y rrr | awk '{ if ($2 ~/y[1-5]{2}y/) print $3}'

Why I cannot get any output?

Thank you.

Aurasphere
  • 3,841
  • 12
  • 44
  • 71
znlyj
  • 1,109
  • 3
  • 14
  • 34

3 Answers3

10

You need to enable "interval expressions" in regular expression matching by specifying either the --posix or --re-interval option.

e.g.

echo xx y11y rrr | awk --re-interval '{ if ($2 ~ /y[1-5]{2}y/) print $3}

From the man page:

--re-interval Enable the use of interval expressions in regular expression matching (see Regular Expressions, below). Interval expressions were not traditionally available in the AWK language. The POSIX standard added them, to make awk and egrep consistent with each other. However, their use is likely to break old AWK programs, so gawk only provides them if they are requested with this option, or when --posix is specified.

dogbane
  • 266,786
  • 75
  • 396
  • 414
0

On my machine:

$ echo xx y11y rrr | awk '{ if ($2 ~/y[1-5]{2}y/) print $3}'
rrr

Was this what you wanted? I'm using GNU awk 4.0.0 in Cygwin on Windows XP.

Eric Wilson
  • 57,719
  • 77
  • 200
  • 270
0

You should force POSIX to use {} in awk

echo xx y11y rrr | awk -W posix '{ if ($2 ~/y[1-5]{2}y/) print $3}'
Ximik
  • 2,435
  • 3
  • 27
  • 53
  • You try to use POSIX-regexp feature, but awk uses it's own regexp format. So u should force using POSIX format of regexp. Also look at dogbane's answer. – Ximik Dec 01 '11 at 14:51