Print only parts that match regex

30,183

Solution 1

If the question is "How can I print only substrings that match specific a regular expression using sed?" then it will be really hard to achieve (and not an obvious solution).

grep could be more helpful in that case. The -o option prints each matching part on a separate line, -P enables PCRE regex syntax:

$> echo "a b _c d _e f" | grep -o -P "(\ *_[a-z]+)"
 _c
 _e

And finally

$> echo `echo "a b _c d _e f" | grep -o -P "(\ *_[a-z]+)"`
_c _e

Solution 2

Identify the patterns you want, surrounded by the patterns you don't want, and emit only those:

echo "a b _c d _e f" | sed 's/[^_]*\s*\(_[a-z]\)[^_]*/\1 /g'

OUTPUT:

_c _e 

Solution 3

Its hacky but you can use this for sed only version:

echo "a b _c d _e f" | sed 's/ /\
/g' | sed -n '/_[a-z]/p'

OUTPUT:

_c
_e
Share:
30,183
Šimon Tóth
Author by

Šimon Tóth

Backend oriented Software developer and Researcher with industry experience starting in 2004.

Updated on January 31, 2020

Comments

  • Šimon Tóth
    Šimon Tóth over 4 years

    echo "a b _c d _e f" | sed 's/[ ]*_[a-z]\+//g'

    The result will be a b d f.

    Now, how can I turn it around, and only print _c _e, while assuming nothing about the rest of the line?

  • Šimon Tóth
    Šimon Tóth about 12 years
    debian grep doesn't seem to have -P compiled in :-/
  • davnicwil
    davnicwil about 7 years
    Also on macOS, use: grep -o -E ...