如何在Unix平台上的文件中grep标签(\t) ?
一种方法是(这是Bash)
grep -P '\t'
-P将打开Perl正则表达式,因此\t将工作。
正如用户unwind所说,它可能是特定于GNU grep的。另一种方法是在shell、编辑器或终端允许的情况下插入一个制表符。
我从来没有设法使“\t”元字符与grep一起工作。 然而,我发现了两个替代方案:
使用<Ctrl-V> <TAB>(按Ctrl-V然后输入TAB) 使用awk: foo | awk '/\t/'
一个好的选择是使用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
(以下答案已根据评论中的建议进行了编辑。谢谢大家)
你可以打字 Grep \t foo 或
grep '\t' foo
在文件foo中搜索制表符。您可能还可以使用其他转义代码,尽管我只测试了\n。虽然这相当耗时,而且不清楚为什么要这样做,但在zsh中,您还可以键入制表符,回到开头,grep并将制表符用引号括起来。
多次查找空格[[:space:]]*
grep[:太空 :]]*'.''.'
会发现这样的东西:
“账单”..
这些是单引号('),而不是双引号(")。这就是在grep中进行连接的方法。= -)
这对于AIX很有效。我正在搜索包含连接<\t>ACTIVE的行
voradmin cluster status | grep JOINED$'\t'ACTIVE
vorudb201 1 MEMBER(g) JOINED ACTIVE
*vorucaf01 2 SECONDARY JOINED ACTIVE
使用'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
编辑:显然,上面的内容只对查看文件内容以定位选项卡有用——如果目标是将选项卡作为更大的脚本会话的一部分来处理,那么这没有任何有用的用途。
下面是问Ubuntu的答案:
告诉grep使用由Perl定义的正则表达式(Perl拥有 \t as tab): grep -P "\t" <文件名称> . grep "\t" <文件名称> . grep "\t 使用文字制表符: grep "^V<tab>" <文件名> . grep "^V<tab> 使用printf打印制表符: Grep "$(printf '\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').
在其他答案中给出的$'\t'符号是特定于shell的——它似乎在bash和zsh中工作,但不是通用的。
注意:下面是针对fish shell的,在bash中不起作用:
在fish shell中,可以使用不带引号的\t,例如:
grep \t foo.txt
或者可以使用十六进制或unicode符号,例如:
grep \X09 foo.txt
grep \U0009 foo.txt
(这些符号对于更深奥的字符很有用)
因为这些值必须是不加引号的,所以可以将加引号的值和不加引号的值进行拼接:
grep "foo"\t"bar"
基本上有两种解决方法:
(Recommended) Use regular expression syntax supported by grep(1). Modern grep(1) supports two forms of POSIX 1003.2 regex syntax: basic (obsolete) REs, and modern REs. Syntax is described in details on re_format(7) and regex(7) man pages which are part of BSD and Linux systems respectively. The GNU grep(1) also supports Perl-compatible REs as provided by the pcre(3) library. In regex language the tab symbol is usually encoded by \t atom. The atom is supported by BSD extended regular expressions (egrep, grep -E on BSD compatible system), as well as Perl-compatible REs (pcregrep, GNU grep -P). Both basic regular expressions and Linux extended REs apparently have no support for the \t. Please consult UNIX utility man page to know which regex language it supports (hence the difference between sed(1), awk(1), and pcregrep(1) regular expressions). Therefore, on Linux: $ grep -P '\t' FILE ... On BSD alike system: $ egrep '\t' FILE ... $ grep -E '\t' FILE ... Pass the tab character into pattern. This is straightforward when you edit a script file: # no tabs for Python please! grep -q ' ' *.py && exit 1 However, when working in an interactive shell you may need to rely on shell and terminal capabilities to type the proper symbol into the line. On most terminals this can be done through Ctrl+V key combination which instructs terminal to treat the next input character literally (the V is for "verbatim"): $ grep '<Ctrl>+<V><TAB>' FILE ... Some shells may offer advanced support for command typesetting. Such, in bash(1) words of the form $'string' are treated specially: bash$ grep $'\t' FILE ... Please note though, while being nice in a command line this may produce compatibility issues when the script will be moved to another platform. Also, be careful with quotes when using the specials, please consult bash(1) for details. For Bourne shell (and not only) the same behaviour may be emulated using command substitution augmented by printf(1) to construct proper regex: $ grep "`printf '\t'`" FILE ...
在表达式中插入制表符的另一种方法是使用Bash中不太为人所知的$'\t'引号:
grep $'foo\tbar' # matches eg. 'foo<tab>bar'
(注意,如果你匹配固定字符串,你可以使用-F模式。)
有时使用变量可以使符号更易于阅读和管理:
tab=$'\t' # `tab=$(printf '\t')` in POSIX
id='[[:digit:]]\+'
name='[[:alpha:]_][[:alnum:]_-]*'
grep "$name$tab$id" # matches eg. `bob2<tab>323`