2015-12-30 90 views
2

我想grepregex但我很难找出我的正则表达式有什么问题。我找不到任何bash正则表达式测试人员,所以这真的很难弄清楚。grep根据正则表达式不匹配任何东西

这里是我的正则表达式

[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

**更新**感谢埃德宾吉

更新的正则表达式:

^[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;[email protected]:/home/ec2-user^G^[[?1034h[[email protected] ec2-user]# cd /data/kafka/tmp/kafka-logs/^M 
^[]0;[email protected]:/data/kafka/tmp/kafka-logs^G[[email protected] 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

+0

你的'[:space:]'只匹配文字字符':space',你需要'[[:space:]]'。以及埃德下面的表示。祝你们好运。 – shellter

回答

3

这是为什么在方括号匹配?

[bbg-sevent-test-] 

如果你匹配整个文本字符串,包括括号,转义:

\[bbg-sevent-test-\] 

如果你不匹配的括号为文字字符,让他们出来:

bbg-sevent-test- 

在我看来,你并不是真的想让他们在那里。在一个正则表达式中,你直接匹配的文本只是在那里放在那里,除了转义特殊字符(如[] * +?())外,不需要特殊的语法。等等。

你在那里得到的是语法上的一个范围 - 但是是一个范围,因为在最后一个连字符之后什么也没有。但是,范围显然不是你的意图。

+1

感谢您的帮助!用正则表达式玩了一下,得到了答案 – Liondancer

相关问题