grep 不支持 Negative Lookahead 问题
刚开始使用printf "123\n24567\n8930\n234\n" | egrep '(?<!2)3'提示egrep: repetition-operator operand invalid, 然后查到可以使用参数-P, 如下:
$ printf "123\n24567\n8930\n234\n" | grep -P '(?<!2)3'
但是仍然报错和上面报错相同, 然后就考虑是不是版本问题,
$ grep --v
grep (BSD grep, GNU compatible) 2.6.0-FreeBSD
尝试更新,
$ brew upgrade grep
Error: grep not installed
然后尝试安装,
$ brew install grep
...
All commands have been installed with the prefix "g".
If you need to use these commands with their normal names, you
can add a "gnubin" directory to your PATH from your bashrc like:
PATH="/opt/homebrew/opt/grep/libexec/gnubin:$PATH"
安装成功, 然后根据提示往.zshrc添加对应环境变量PATH, 注意$:PATH的含义(追加),
查看版本,
grep --v
grep (GNU grep) 3.10
Packaged by Homebrew
Copyright (C) 2023 Free Software Foundation, Inc.
现在支持上面语法grep -P '(?<!2)3' , 问题解决~
查看其他文章