【问题标题】:“grep -oe” works, but not “grep -eo”“grep -oe”有效,但“grep -eo”无效
【发布时间】:2021-08-21 23:57:04
【问题描述】:

在我的终端提示符下, echo 'Banana' | grep -oe 'n' 按预期工作:

$ echo 'Banana' | grep -oe 'n'
n
n

但不是 echo 'Banana' | grep -eo 'n'

$ echo 'Banana' | grep -eo 'n'
grep: n: No such file or directory

为什么 -e &-o的顺序很重要?

【问题讨论】:

  • -eo 的意思是“查找正则表达式o。下面的n 被解释为文件名。非posix 标志可能在它们' 被捆绑。重要的原因是,紧跟在 -e 之后的下一件事将被解释为正则表达式。让它以任何其他方式运行将是毁灭性的。
  • -e 后面必须紧跟搜索模式,所以如果你想把o 放在e 之后,你需要grep -e 'n' -o
  • 现在我知道你是对的:-e 表示接下来是模式;因此-e 必须排在最后;这允许以- 开头的模式。 $ echo '香蕉' | grep -e -o 'n' grep: n: 没有这样的文件或目录 $ echo 'Banana' | grep -o -e 'n' n n 手册页指的是这个。例如,$ echo '高端' | grep -e -e high-end 但不是 100% 正确:手册页上说 -e-f 可以组合使用。考虑: $ echo 'dog-food' | grep -e -f ood grep: ood: 没有这样的文件或目录但是: $ touch a.txt $ echo 'high-end' | grep -e -e a.txt $ echo '高端' | grep -e -e -f a.txt 高端

标签: shell grep


【解决方案1】:

请注意下面附加的输出。 -e 用于将 o 视为文件 n 中的表达式的表达式。

【讨论】:

  • 现在我知道你是对的:-e 表示接下来是模式;因此-e 必须排在最后,允许以- 开头的模式。 $ echo 'Sand' | grep -e -o n grep: n: No such file or directory $ echo 'Sand' | grep -o -e n n 手册页指的是这个。例如,$ echo 'high-end' | grep -e -ehigh-end 不是 100% 正确:手册页上说 -e&-f 可以组合使用。考虑:$ echo 'dog-f ood' | grep -e -f oodgrep: ood: No such file or directory 但是:$ touch a.txt$ echo 'high-end' | grep -e -e a.txt$ echo 'high-end' | grep -e -e -f a.txthigh-end
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-12-05
  • 2018-06-16
  • 2014-04-09
  • 2011-07-09
  • 2015-07-08
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多