awk和正则表达式的简单问题

时间:2022-10-25 16:05:31
 echo xx y11y rrr | awk '{ if ($2 ~/y[1-5]{2}y/) print $3}'

Why I cannot get any output?

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

Thank you.

谢谢。

3 个解决方案

#1


7  

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

您需要通过指定--posix或--re-interval选项在正则表达式匹配中启用“区间表达式”。

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.

--re-interval在正则表达式匹配中启用区间表达式(请参阅下面的正则表达式)。 AWK语言中传统上不提供区间表达式。 POSIX标准添加了它们,使awk和egrep相互一致。但是,它们的使用可能会破坏旧的AWK程序,因此如果使用此选项请求它们,或者指定了--posix,则gawk仅提供它们。

#2


0  

You should force POSIX to use {} in awk

你应该强迫POSIX在awk中使用{}

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

#3


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.

这是你想要的吗?我在Windows XP上使用Cygwin中的GNU awk 4.0.0。

#1


7  

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

您需要通过指定--posix或--re-interval选项在正则表达式匹配中启用“区间表达式”。

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.

--re-interval在正则表达式匹配中启用区间表达式(请参阅下面的正则表达式)。 AWK语言中传统上不提供区间表达式。 POSIX标准添加了它们,使awk和egrep相互一致。但是,它们的使用可能会破坏旧的AWK程序,因此如果使用此选项请求它们,或者指定了--posix,则gawk仅提供它们。

#2


0  

You should force POSIX to use {} in awk

你应该强迫POSIX在awk中使用{}

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

#3


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.

这是你想要的吗?我在Windows XP上使用Cygwin中的GNU awk 4.0.0。