Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bash regex finding particular words in a sentence

I have a sentence like this:

"The dog jumped over the moon because he likes jumping"

And I want to find all words that match jump.*, i.e. jumped and jumping. How can I do this?

Currently I have the sentence in a variable, $sentence. And I know the match word that I want to test against, e.g. $test is jump.

Thank you

like image 727
JDS Avatar asked Sep 12 '25 14:09

JDS


2 Answers

A Pipe-Free Bash Solution

If you want to do this purely in Bash, you can use the regular expression matching operator and the built-in BASH_REMATCH variable to hold the results. For example:

re='\bjump[[:alpha:]]*\b'
string="The dog jumped over the moon because he likes jumping"
for word in $string; do
    [[ "$word" =~ $re ]] && echo "${BASH_REMATCH}"
done

Given your corpus, this correctly returns the following results:

jumped
jumping
like image 177
Todd A. Jacobs Avatar answered Sep 14 '25 05:09

Todd A. Jacobs


Try this regex:

/\bjump.*?\b/

See here. \b matches word boundaries and jump.*? everything between that starts with jump.

In bash you can use it with grep:

echo $sentence | grep -oP "\b$test.*?\b"
like image 40
morja Avatar answered Sep 14 '25 05:09

morja