grep 制表符和星号

dov*_*vah 2 grep

如何*在文本文件中搜索制表符和星号 ( ) 字符的组合?

例如:

输入:

text    *    0    *    0    *    *    some_text
text    *    9    45   9    0    0    some_text
TEXT    *    0    *    0    0    *    some_text
Run Code Online (Sandbox Code Playgroud)

我需要为制表符和星号和零的特定组合进行 grep,例如:

*    0    *    0    0    *
Run Code Online (Sandbox Code Playgroud)

预期输出:

TEXT    *    0    *    0    0    *    some_text
Run Code Online (Sandbox Code Playgroud)

我可以分别使用 grep 查找星星:

grep -P '\t' input > output
Run Code Online (Sandbox Code Playgroud)

我可以单独使用 grep 选项卡:

grep '\*' input > output
Run Code Online (Sandbox Code Playgroud)

但是我怎样才能把两者结合起来呢?我正在尝试以下组合,但未成功:

grep -P '\*\t0\t\*0\t0\*' input > output
Run Code Online (Sandbox Code Playgroud)

Sté*_*las 5

便携:

tab=$(printf '\t')
grep -F "*${tab}0${tab}*${tab}0${tab}0"
Run Code Online (Sandbox Code Playgroud)

使用某些 shell ( ksh93, zsh, bash, mksh, FreeBSD sh),您可以使用:

grep -F $'*\t0\t*\t0\t0'
Run Code Online (Sandbox Code Playgroud)

($'\t'也可以写成$'\u0009'or (在基于 ASCII 的系统上) $'\x09', $'\11'or $'\CI')

一些grep实现,如 ast-open 的实现,将\t(或\x09)自身识别为制表符。所以你可以这样做:

grep '\*\t0\t\*\t0\t0'
Run Code Online (Sandbox Code Playgroud)

(与那里的其他正则表达式类型相同(-E对于 ERE,-P对于类似 perl(类似于 PCRE),-A对于增强)。

GNU grep(至少在GNU系统)不承认\t,也不\x09与BRE或ERE,但PCREs做(当支持已内置),(也\x09\11)。

grep -P '\*\t0\t\*\t0\t0'
Run Code Online (Sandbox Code Playgroud)

grep只要启用了 PCRE 支持(在现代系统上往往就是这种情况),它就可以与 GNU一起使用。

另一种便携式解决方案是使用awk,而不是进行了\t普遍的支持:

awk '/\*\t0\t\*\t0\t0/'
Run Code Online (Sandbox Code Playgroud)