Grep – how to output only the content of a capturing group

This question was asked ten years ago, so I won’t mark it as duplicate. Also I noticed no sed solution was given since OP asked an answer without:

sed -nr 's/(hello[0-9]+), please match me/\1/p' test.txt
  • -n stands for quiet (won’t print anything except if explicitly asked)
  • -r allows use of extented regular expressions (avoids here using \ before parenthesis)
  • s/reg/repl/p command means “if regexp reg matches the current line, replace it by captured text by repl, and prints it (/p)”

Leave a Comment