如何在Bash中使用反向引用

met*_*dos 8 regex unix grep text-processing

我有一个带有反向引用的正则表达式.如何在bash脚本中使用它?

比如我想打印匹配的内容(.*)

grep -E "CONSTRAINT \`(.*)\` FOREIGN KEY" temp.txt 
Run Code Online (Sandbox Code Playgroud)

如果应用它

CONSTRAINT `fk_dm` FOREIGN KEY
Run Code Online (Sandbox Code Playgroud)

我想输出

fk_dm
Run Code Online (Sandbox Code Playgroud)

kev*_*kev 13

$ echo 'CONSTRAINT `helloworld` FOREIGN KEY' | grep -oP '(?<=CONSTRAINT `).*(?=` FOREIGN KEY)'
helloworld
Run Code Online (Sandbox Code Playgroud)
-o, --only-matching       show only the part of a line matching PATTERN
-P, --perl-regexp         PATTERN is a Perl regular expression
Run Code Online (Sandbox Code Playgroud)
(?=pattern)
    is a positive look-ahead assertion
(?!pattern)
    is a negative look-ahead assertion
(?<=pattern)
    is a positive look-behind assertion
(?<!pattern)
    is a negative look-behind assertion 
Run Code Online (Sandbox Code Playgroud)