Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
528 views
in Technique[技术] by (71.8m points)

grep regex whitespace behavior

I have a textfile, containing something like:

12,34 EUR 
 5,67 EUR
 ...

There is one whitespace before 'EUR' and I ignore 0,XX EUR.

I tried:

grep '[1-9][0-9]*,[0-9]{2}sEUR' => didn't match !

grep '[1-9][0-9]*,[0-9]{2} EUR' => worked !

grep '[1-9][0-9]*,[0-9]{2}s*EUR' => worked !

grep '[1-9][0-9]*,[0-9]{2}s[E]UR' => worked !

Can somebody explain me pls, why I can't use s but s* and s[E] matched?

OS: Ubuntu 10.04, grep v2.5

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

This looks like a behavior difference in the handling of s between grep 2.5 and newer versions (a bug in old grep?). I confirm your result with grep 2.5.4, but all four of your greps do work when using grep 2.6.3 (Ubuntu 10.10).

Note:

GNU grep 2.5.4
echo "foo bar" | grep "s"
   (doesn't match)

whereas

GNU grep 2.6.3
echo "foo bar" | grep "s"
foo bar

Probably less trouble (as s is not documented):

Both GNU greps
echo "foo bar" | grep "[[:space:]]"
foo bar

My advice is to avoid using s ... use [ ]* or [[:space:]] or something like it instead.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...