如何在Grep中使用向后引用

问题描述 投票:8回答:2

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

例如,我要打印与(。*)相匹配的内容

grep -E "CONSTRAINT \`(.*)\` FOREIGN KEY" temp.txt 

如果适用于

CONSTRAINT `fk_dm` FOREIGN KEY

我要输出

fk_dm
regex unix grep text-processing
2个回答
13
投票
$ 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 

-2
投票
grep -E 'CONSTRAINT \`(.*)\` FOREIGN KEY' temp.txt 
© www.soinside.com 2019 - 2024. All rights reserved.