如何在Unix平台上的文件中grep标签(\t) ?


当前回答

grep "$(printf '\t')"在Mac OS X上为我工作

其他回答

These alternative binary identification methods are totally functional. And, I really like the one's using awk, as I couldn't quite remember the syntaxic use with single binary chars. However, it should also be possible to assign a shell variable a value in a POSIX portable fashion (i.e. TAB=echo "@" | tr "\100" "\011"), and then employ it from there everywhere, in a POSIX portable fashion; as well (i.e grep "$TAB" filename). While this solution works well with TAB, it will also work well other binary chars, when another desired binary value is used in the assignment (instead of the value for the TAB character to 'tr').

一种方法是(这是Bash)

grep -P '\t'

-P将打开Perl正则表达式,因此\t将工作。

正如用户unwind所说,它可能是特定于GNU grep的。另一种方法是在shell、编辑器或终端允许的情况下插入一个制表符。

一个好的选择是使用sed。

sed -n '/\t/p' file

示例(工作在bash, sh, ksh, csh,..):

[~]$ cat testfile
12 3
1 4 abc
xa      c
        a       c\2
1 23

[~]$ sed -n '/\t/p' testfile 
xa      c
        a       c\2
[~]$ sed -n '/\ta\t/p' testfile
        a       c\2

(以下答案已根据评论中的建议进行了编辑。谢谢大家)

这对于AIX很有效。我正在搜索包含连接<\t>ACTIVE的行

voradmin cluster status | grep  JOINED$'\t'ACTIVE

 vorudb201   1       MEMBER(g) JOINED        ACTIVE
*vorucaf01   2       SECONDARY JOINED        ACTIVE

如果使用GNU grep,你可以使用perl风格的regexp:

grep -P '\t' *