bashshellsed

Shorten multiple patterns that has a prefix


I use the following sed command to delete all lines except that match containing patterns:

sed '/orders="Green\|orders="Orange\|orders="Blue/!d'

This works, but how do you make it shorter so that orders=" is only used one time?

sed '/orders="["Green","Orange","Blue"]/!d'

Something like this matches any of the characters between brackets, but ignores the whole word.


Solution

  • sed line selection uses regular expression, and by default, without the -E/-r option, the Basic flavor is used.

    What you want to use is Groups - \(regexp\)

    So something like this:

    sed '/orders="\(Green\|Orange\|Blue\)/!d'