Simple trouble with awk and regex

前端 未结 3 1237
半阙折子戏
半阙折子戏 2021-01-13 04:57
 echo xx y11y rrr | awk \'{ if ($2 ~/y[1-5]{2}y/) print $3}\'

Why I cannot get any output?

Thank you.

3条回答
  •  梦毁少年i
    2021-01-13 05:12

    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.

提交回复
热议问题