反引号之间的grep字符串

TKH*_*KHN 4 grep regular-expression

`someString` complete(1)
Run Code Online (Sandbox Code Playgroud)

我需要在反引号之间的“someString”。在下面尝试但不起作用。

cat file.txt | grep '\`.*\`'
Run Code Online (Sandbox Code Playgroud)

αғs*_*нιη 5

使用grep支持PCRE扩展的GNU :

grep -Po '(?<=`)[^`]*(?=`)' infile
Run Code Online (Sandbox Code Playgroud)

或返回 a并且c仅例如在 中`a`b`c`,您将执行以下操作:

grep -Po '(?<=`)[^`]+(?=`(?:[^`]*`[^`]*`)*[^`]*$)'
Run Code Online (Sandbox Code Playgroud)

返回一对反引号之间的所有内容。

温馨提示:
(?<=...):正向后看。
(?=...):正面展望。
(?:...):非捕获组。
[^`]*: 除反引号外的任何字符`


与awk:

awk -F'`' '{ for(i=2; i<=NF; i+=2) print $i; }' infile
Run Code Online (Sandbox Code Playgroud)