如何在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').
其他回答
+1方式,工作在ksh,破折号等:使用printf插入制表符:
grep "$(printf 'BEGIN\tEND')" testfile.txt
如果使用GNU grep,你可以使用perl风格的regexp:
grep -P '\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
(以下答案已根据评论中的建议进行了编辑。谢谢大家)
在其他答案中给出的$'\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"
下面是问Ubuntu的答案:
告诉grep使用由Perl定义的正则表达式(Perl拥有 \t as tab): grep -P "\t" <文件名称> . grep "\t" <文件名称> . grep "\t 使用文字制表符: grep "^V<tab>" <文件名> . grep "^V<tab> 使用printf打印制表符: Grep "$(printf '\t')"<文件名>
推荐文章
- 在Windows中有像GREP这样的模式匹配实用程序吗?
- 如何从命令行将每两行合并为一行?
- 如何从命令行通过mysql运行一个查询?
- 在创建守护进程时执行双fork的原因是什么?
- 匹配前后的Grep字符?
- (grep)正则表达式匹配非ascii字符?
- 如何从另一个文件A中删除文件B中出现的行?
- 对以制表符分隔的文件进行排序
- 如何使用查找命令从列表中查找所有具有扩展名的文件?
- 如何将文件指针(file * fp)转换为文件描述符(int fd)?
- 在Bash中获取日期(比当前时间早一天)
- Linux: kill后台任务
- 在OSX中永久设置PATH环境变量
- PowerShell等价于grep -f
- 如何在C程序中获取当前目录?