2015-12-30 173 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

相關問題