Last edit


< sed -n '/\(foo\)\(bar\).*\2\1/p' # prints lines with "foobar" and "barfoo" later


> sed -n '/\(foo\)\(bar\).*\2\1/p' # prints lines with "foobar" and "barfoo" later in the line

The usual (and correct) answer for backreferences in awk (for example, the answer you can get on #awk for this question) is: "you can't do backreferences in awk". That is only partly true.

If you need to match a pattern using a regular expression with backreferences, like eg you do in sed

sed -n '/\(foo\)\(bar\).*\2\1/p'  # prints lines with "foobar" and "barfoo" later in the line

or similar things, then well, you can't do that easily with awk.

But if you are using backreferences during string substitution, to insert text previously captured by a capture group, then you will almost certainly be able to get what you want with awk. Following are some hints:

echo 'foo123bar' | sed 's/.*\([0-9]\{1,\}\).*/\1/'
echo 'blah <a href="">blah blah</a>' | sed 's/.*"\([^"]*\)".*/\1/'

Both things can be done in awk (and sed as well!) without the need of backreferences. You just delete the part of the line you don't need:

awk '{gsub(/^[a-z]*|[a-z]*$/,"");print}'   # 1st example
awk '{gsub(/^[^"]*"|"[^"]*$/,"");print}'   # 2nd example

Generally speaking, however, the above methods (both sed and awk) require that you have only one matching substring to extract per line.