简体   繁体   中英

grep for words ending in 'ing' immediately after a comma

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM