如何在Unix平台上的文件中grep标签(\t) ?
当前回答
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').
其他回答
使用gawk,将字段分隔符设置为TAB (\t)并检查字段的数量。如果多于1,则有/有制表符
awk -F"\t" 'NF>1' file
诀窍是在单引号前使用$符号。它也适用于切割和其他工具。
grep $'\t' sample.txt
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').
你可能想使用grep "$(echo -e '\t')"
唯一的要求是echo能够解释反斜杠转义。
使用'sed-as-grep'方法,但是用个人偏好的可见字符替换制表符是我最喜欢的方法,因为它清楚地显示了哪些文件包含所请求的信息,以及它在行的位置:
sed -n 's/\t/\*\*\*\*/g' file_name
如果您希望使用行/文件信息,或其他grep选项,但也希望看到制表符的可见替换,您可以通过
grep -[options] -P '\t' file_name | sed 's/\t/\*\*\*\*/g'
举个例子:
$ echo "A\tB\nfoo\tbar" > test
$ grep -inH -P '\t' test | sed 's/\t/\*\*\*\*/g'
test:1:A****B
test:2:foo****bar
编辑:显然,上面的内容只对查看文件内容以定位选项卡有用——如果目标是将选项卡作为更大的脚本会话的一部分来处理,那么这没有任何有用的用途。