質問

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.

役に立ちましたか?

解決

Try ,\s*\S+ing

Matches your first two phrases, doesn't match in your third phrase.

\s means 'any whitespace', * means 0 or more of that, \S means 'any non-whitespace' (capitalizing the letter is conventional for inverting the character set in regexes - works for \b \s \w \d), + means 'one or more' and then we match ing.

他のヒント

You can use the \b token to match on word boundaries (see this page).

Something like the following should work:

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

EDIT: Except now I realised that the \b is unnecessary, and .*,\s*\w*ing would work, as Patashu pointed out. My regex-fu is rusty.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top