beta
beta

Reputation: 5686

Regex to match a word followed by another word with a certain suffix

I need a grep regex which I want to run over a text file. The grep command should output each line of the text file which contains a word (let that word be foo) followed by a space and another word which ends with a certain suffix (let that suffix be ing).

So the regex should match sequences such as: foo going, foo walking, foo running

Please not that the ing should be in the next word after foo. So the regex should not match: foo asfd asdfing.

I tried

grep -i "foo .*ing"

but this matches not only on the immediate next word, it also matches if ing is in the second word after the foo.

Upvotes: 3

Views: 10045

Answers (5)

Michael Gaskill
Michael Gaskill

Reputation: 8042

I went with this short script using egrep instead of grep:

egrep -i "foo\s+\w+ing\b" foo.txt

which is directly equivalent to this:

grep -Ei "foo\s+\w+ing\b" foo.txt

The input file (poetry, really) that I came up with looks like this:

I was a little under the weather,
so I decided to foo going, and that involved
a lot of foo walking, and maybe just a little
bit of foo running, but certainly no foo
dancing or anything like that, because nobody wants
to be caught dancing any kind of way, especially
the foo way!

The output is as follows:

so I decided to foo going, and that involved
a lot of foo walking, and maybe just a little
bit of foo running, but certainly no foo

This was what I expected to see based on my reading of the description.

Upvotes: 1

Federico Piazza
Federico Piazza

Reputation: 31005

Assuming that your words can be case insensitive you can leverage the insensitive flag (?i)

You can use a regex like this:

(?i)foo [a-z]*ing

Working demo

Btw, if multiple spaces can exist between foo and the next word you can use:

(?i)foo\s+[a-z]*ing

And if you consider that ing is not a word, then you can improve above to:

(?i)foo\s+[a-z]+ing

Regular expression visualization

If you want grep to support Perl regex you can use:

grep -P '(?i)foo\s+[a-z]+ing'

Upvotes: 5

anubhava
anubhava

Reputation: 785266

You can use this grep command to match a word foo and next word ending with ing:

grep -Ew 'foo[[:blank:]]+[^[:blank:]]+ing' file

Testing:

cat <<'EOF' | grep -Ew 'foo[[:blank:]]+[^[:blank:]]+ing'
pqr foo going
bar foo walking
bar tofoo walking
foo running
abc def foo asdf asdfing
EOF

Output:

pqr foo going
bar foo walking
foo running

Upvotes: 2

Sebastian Proske
Sebastian Proske

Reputation: 8413

You could use grep -i "\bfoo [a-z]+ing\b" It uses word boundaries to ensure, that the word is just foo, but not kafoo and the following word is actually ending with ing, like finding but not binged

Upvotes: 2

Felippe Duarte
Felippe Duarte

Reputation: 15131

If you use .* will match everything, this will only match text:

"foo\s[a-zA-Z]+ing"

Upvotes: 2

Related Questions