Q:

Search and replace with back reference variables

String text = "This is a very tiny test!!!"    
Pattern p = "this is a (.*) test.*"

Output o = "very tiny" (after search and replace)

I want to replace the pattern with the content inside the bracket (), which is indexed with $1. On Linux, what command can do this and how?
I have to do this at command line, since my file is several GB and can't be done in an editor.

A:

Using GNU sed:
s='This is a very tiny test!!!'
sed -En 's/this is a (.*) test.*/\1/ip' <<< $s

The output:
very tiny

i - The I modifier to regular-expression matching is a GNU extension which makes sed match regexp in a case-insensitive manner.

Alternative perl approach:
perl -nle 'print $1 if /this is a (.*) test.*/i' <<< $s

