0

I am trying to grep files for lines with a word ending in 'ing' immediately after a comma, of the form:

... we gave the dog a bone, showing great generosity ...
... this man, having no home ...

but not:

... this is a great place, we are having a good time ...

I would like to find instances where the 'ing' word is the first word after a comma. It seems like this should be very doable in grep, but I haven't figured out how, or found a similar example.

I have tried

grep -e ", .*ing"

which matches multiple words after the comma. Commands like

grep -i -e ", [a-z]{1,}ing"
grep -i -e ", [a-z][a-z]+ing"

don't do what I expect--they don't match phrases like my first two examples. Any help with this (or pointers to a better tool) would be much appreciated.

4

2 回答 2

6

尝试,\s*\S+ing

匹配你的前两个短语,不匹配你的第三个短语。

\s表示“任何空白”,* 表示 0 或更多,\S表示“任何非空白”(大写字母是反转正则表达式中字符集的惯例 - 适用于\b \s \w \d),+表示“一个或多个”,然后我们匹配ing

于 2013-05-09T22:51:41.960 回答
3

您可以使用\b标记来匹配单词边界(请参阅此页面)。

像下面这样的东西应该可以工作:

grep -e ".*, \b\w*ing\b"

编辑:除了现在我意识到这\b是不必要的,并且.*,\s*\w*ing会起作用,正如 Patashu 指出的那样。我的正则表达式生锈了。

于 2013-05-09T22:53:42.517 回答