Skip to content

sed

Using sed to extract text from files

sed -n -r 's/.*?something (.*?) here.*/\1/p'

Notes:

  • sed will take its input from stdin
  • sed regex does not support \d , use [0-9] instead to match a digit

A breakdown of the sed "script" above:

  • -n : suppress printing of pattern space
  • -r : use regular expressions in the sed "script"
  • s/ : search and substitute
  • .*? : discard everything before the matching pattern
  • (.*?) : capture group for the text we want to extract
  • .* : discard everything after the matching regex pattern
  • \1 : the value of the capture group
  • /p : prints the matched capture group (represented by \1 )