开发者

shell command: need help about a regex match

开发者 https://www.devze.com 2023-01-12 22:32 出处:网络
I have a sentence that I want to write a shell command to grep it from a text: The sentence is: self.timeout=2.0

I have a sentence that I want to write a shell command to grep it from a text: The sentence is:

self.timeout=2.0

However, as this is a part of code from a file. so it this sentence could also be

self.timeout = 2.0

or

self.timeout =2.0

or

self.timeout = 8.0

that i开发者_运维知识库s: there may be blanks besides "=", and the value of self.timeout maybe different.

So could anybody help to give me a regex in shell command. Anyway, I know the shell:

grep "self.timeout*="

works. But I think it is not a good regex in shell command.

Thanks a lot!


I'd do:

grep -E 'self\.timeout[ \t]*=[ \t]*[0-9.]+'

note:

      |      |          |  |           
  use egrep  |          | zero or more
             |    whitespace
             |
  make sure we're matching
     a dot instead of
      "any character"     


Using grep -E aka egrep you can use a regular expression, with which the * operator will match 0 or more of the preceding character:

egrep 'self\.timeout *='

Or use [[:space:]] to match all whitespace characters:

egrep 'self\.timeout[[:space:]]*='
0

精彩评论

暂无评论...
验证码 换一张
取 消