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


当前回答

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

其他回答

你可以打字 Grep \t foo 或

grep '\t' foo

在文件foo中搜索制表符。您可能还可以使用其他转义代码,尽管我只测试了\n。虽然这相当耗时,而且不清楚为什么要这样做,但在zsh中,您还可以键入制表符,回到开头,grep并将制表符用引号括起来。

使用gawk,将字段分隔符设置为TAB (\t)并检查字段的数量。如果多于1,则有/有制表符

awk -F"\t" 'NF>1' file

诀窍是在单引号前使用$符号。它也适用于切割和其他工具。

grep $'\t' sample.txt

一个好的选择是使用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

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

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

grep -P '\t' *