【问题标题】:grep based on regex not matching anything基于正则表达式的 grep 不匹配任何内容
【发布时间】:2015-12-30 00:34:28
【问题描述】:

我正在尝试通过regex 发送grep,但我无法弄清楚我的正则表达式有什么问题。我找不到任何 bash 正则表达式测试人员,所以这真的很难弄清楚。

这是我的正则表达式

[0-9]*\.[0-9]*[G][:space:]*\.\/[bbg-sevent-test-][0-9]*

我正在尝试将我的正则表达式与这段文字相匹配

2.0G ./bbg-sevent-test-132^M

我正在运行的命令是:

./kafka_prefill.sh | sed -r "s/\x1B\[([0-9]{1,2}(;[0-9]{1,2})?)?[m|K]//g" | grep '[0-9]*\.[0-9]*[G][:space:]*\.\/[bbg-sevent-test-][0-9]*' > data3.txt

它的作用是运行我的脚本,翻译/删除部分输出,然后基于正则表达式 grep 并将其放入文件 data3.txt

我目前收到此错误:

grep: Invalid range end

** 更新**感谢 Ed Plunkett

更新的正则表达式:

^[0-9]*\.[0-9]*[G][[:space:]]*\.\/bbg-sevent-test-[0-9]*$

我的命令不再有正则表达式错误。然而,没有什么是匹配的。这是一个示例输出:

********************************************************************************^M
This is a private computer system containing information that is proprietary^M
and confidential to the owner of the system.  Only individuals or entities^M
authorized by the owner of the system are allowed to access or use the system.^M
Any unauthorized access or use of the system or information is strictly^M
prohibited.^M
^M
All violators will be prosecuted to the fullest extent permitted by law.^M
********************************************************************************^M
Last login: Tue Dec 29 16:43:23 2015 from 10.81.64.204^M^M
sudo bash^M
cd /data/kafka/tmp/kafka-logs/^M
du -kh . | egrep "bbg-sevent-test-*"^M
-bash: ulimit: open files: cannot modify limit: Operation not permitted^M
### Trinity env = prod ###^M
### Kafka Broker Id = 1 ###^M
### Kafka Broker must be started as root!! ###^M
exit^M
exit^M
### Trinity env = prod ###^M
### Kafka Broker Id = 1 ###^M
### Kafka Broker must be started as root!! ###^M
^[]0;root@ip-10-81-66-20:/home/ec2-user^G^[[?1034h[root@ip-10-81-66-20 ec2-user]# cd /data/kafka/tmp/kafka-logs/^M
^[]0;root@ip-10-81-66-20:/data/kafka/tmp/kafka-logs^G[root@ip-10-81-66-20 kafka-logs]# du -kh . | egrep "bbg-sevent-test-*"^M
2.2G    ./bbg-sevent-test-439^M
2.2G    ./bbg-sevent-test-638^M
2.2G    ./bbg-sevent-test-679^M
2.2G    ./bbg-sevent-test-159^M

我只是想匹配这个位

2.2G ./bbg-sevent-test-159

【问题讨论】:

  • 你的[:space:] 只匹配文字字符:space,你需要[[:space:]]。下面的 Ed 也表明了这一点。祝大家好运。

标签: regex bash shell sed grep


【解决方案1】:

为什么要放在方括号里?

[bbg-sevent-test-]

如果您要匹配整个文字字符串,包括括号,请将它们转义:

\[bbg-sevent-test-\]

如果您没有将括号作为文字字符进行匹配,请将它们排除在外:

bbg-sevent-test-

在我看来,您并不真的希望他们在那里。在正则表达式中,您按字面意思匹配的文本只是按原样插入,除了转义特殊字符(如 []*+?())外,不需要特殊语法。等等。

从句法上讲,您所拥有的是一个范围——但是一个损坏的范围,因为在最后一个连字符之后什么都没有。但是,范围显然不是您的意图。

【讨论】:

  • 感谢您的帮助!玩了一下正则表达式,得到了答案
猜你喜欢
  • 1970-01-01
  • 2017-02-26
  • 2017-02-09
  • 2018-10-01
  • 1970-01-01
  • 1970-01-01
  • 2012-01-21
  • 2016-02-21
相关资源
最近更新 更多