Yeyeye
Yeyeye

Reputation: 29

Add bracket around a string that match this pattern

Using sed commands

In the following sentence add [] around words starting with s and containing e and t in any order “subtle exhibit asset sets tests site”.

Here is what I have so far (see code section)

$ echo "subtle exhibit asset sets tests site." | sed 's/^s\ "et"/[]/g'

Upvotes: 0

Views: 105

Answers (1)

Ed Morton
Ed Morton

Reputation: 203502

This may be what you're looking for, using GNU sed for -E so we don't need to escape the ( and ) capture group delimiters and can use | for "or", and \w shorthand for word-constituent characters and \< word boundary:

$ echo 'subtle exhibit asset sets tests site.' |
    sed -E 's/\<s\w*(e\w*t|t\w*e)\w*/[&]/g'
[subtle] exhibit asset [sets] tests [site].

Upvotes: 1

Related Questions