Extract regular expression group match using grep or sed

I’ve been looking for a GNU/Linux command that matches lines in a text file using a regular expression and extracts a group match for that expression (a sub-match within parenthesis).

http://www.mikeplate.com/2012/05/09/extract-regular-expression-group-match-using-grep-or-sed/


echo 'CREATE TABLE `offercalc_fields` (' | sed -nr 's/^CREATE TABLE `(.+)\_(.*)`.*/first_word='\1', second_word='\2'/p'


cat file-permission.log | sed -nr "s/^mode of \`(.*)' changed from (....).*/chmod -c \2 \1/p"

zcat -f *.gz | sed -nr 's/^.*found (.*)\@\:11.22.33.44.*/\1/p' | sort | uniq

zcat -f exceptions.*.log.gz |grep 'exception of' | sed -nr 's/^\[([[:digit:]]+-[[:digit:]]+-[[:digit:]]+) ([[:digit:]]+).*/\1 \2/p' | uniq

zcat -f exceptions.* |grep 'exception of' | sed -nr 's/^\[([[:digit:]]+-[[:digit:]]+-[[:digit:]]+) ([[:digit:]]+).*/\1 \2/p' | uniq


zcat -f /var/log/squid/squid-access.log | grep -iv 'cache' | sed -nr 's/^.*\|.*\|.*\[(..\/...\/....):(..:..).*/\1 \2/p' | uniq

zcat -f /var/log/squid/squid-access.log | grep -iv 'cache' | sed -nr 's/^.*\[(..\/...\/....):(..:..).*/\1 \2/p' | uniq