使用awk和regex的简单麻烦

使用awk和regex的简单麻烦

问题描述:

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

为什么我无法获得任何输出?

Why I cannot get any output?

谢谢.

您需要通过指定--posix--re-interval选项在正则表达式匹配中启用间隔表达式".

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

例如

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

在手册页中:

-重新间隔 在正则表达式匹配中启用间隔表达式的使用(请参见下面的正则表达式).间隔表达式为 传统上无法在 AWK语言. POSIX标准添加了它们,以使awk和egrep彼此一致.但是,它们的用途是 可能会破坏旧的AWK程序,因此 gawk仅在要求使用此选项或指定了--posix时才提供它们.

--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.