Home > OS >  Add bracket around a string that match this pattern
Add bracket around a string that match this pattern

Time:02-05

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'

CodePudding user response:

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].

CodePudding user response:

Using GNU sed

$ sed 's/\<s\([a-z]*\?e[a-z]*\?t[a-z]*\?\|[a-z]*\?t[a-z]*\?e[a-z]*\?\)[^ ]*\>/[&]/g' input_file
[subtle] exhibit asset [sets] tests [site].
  •  Tags:  
  • Related