关于在sed和awk中使用\b,\d不成功的问题

windows下安装的git-bash,连接到testing的linux环境下,执行,如图所示
image
只有第一行执行成功了,echo 233 456|sed -n “/\b233\b/p”,成功输出结果;
剩下4行命令,都没有成功输出结果,是什么原因?
echo 233 456|sed -n “/\d{3}/p”
echo 233 456|sed -nE “/\d{3}/p”
echo 233 456|awk ‘/\d{3}/’
echo 233 456|awk ‘/\b233\b/’

提前谢过各位老师和同学啦~

参考资料

正则表达式分类

问题解答

  • 一般的扩展正则并不支持\d参数 所以导致前三个命令不成功 详见前面的参考资料内容
  • \b参数在awk的正则表达式里貌似不生效,具体原因我也没查到。。不知道有没有别的大佬了解的可以回答一下。。
  • 另外其实你可以不用太纠结这个,我建议混合使用这三剑客,用每个命令的长处来处理对应的问题,这样才能省时省力。

你怎么知道 awk 有 \d 和 \b 的?官方文档没有说支持这个,并且文档已经说的很清楚了,如果想匹配单词/数字,可以用 /[A-Za-z0-9]/ ,不过它们更推荐POSIX语法 /[[:alnufm:]]/ ,遇到问题,不要猜原因,看文档不快乐吗 :partying_face:

For example, before the POSIX standard, to match alphanumeric characters, you would have had to write /[A-Za-z0-9]/. If your character set had other alphabetic characters in it, this would not match them, and if your character set collated differently from ASCII, this might not even match the ASCII alphanumeric char‐acters. With the POSIX character classes, you can write /[[:alnum:]]/, and this matches the alphabetic and numeric charac‐ters in your character set, no matter what it is.