我有一个带有后向引用的正则表达式。如何在bash脚本中使用它?
例如,我要打印与(。*)相匹配的内容
grep -E "CONSTRAINT \`(.*)\` FOREIGN KEY" temp.txt
如果适用于
CONSTRAINT `fk_dm` FOREIGN KEY
我要输出
fk_dm
$ echo 'CONSTRAINT `helloworld` FOREIGN KEY' | grep -oP '(?<=CONSTRAINT `).*(?=` FOREIGN KEY)'
helloworld
-o, --only-matching show only the part of a line matching PATTERN
-P, --perl-regexp PATTERN is a Perl regular expression
(?=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
grep -E 'CONSTRAINT \`(.*)\` FOREIGN KEY' temp.txt