如何在Unix平台上的文件中grep标签(\t) ?
当前回答
你可以打字 Grep \t foo 或
grep '\t' foo
在文件foo中搜索制表符。您可能还可以使用其他转义代码,尽管我只测试了\n。虽然这相当耗时,而且不清楚为什么要这样做,但在zsh中,您还可以键入制表符,回到开头,grep并将制表符用引号括起来。
其他回答
诀窍是在单引号前使用$符号。它也适用于切割和其他工具。
grep $'\t' sample.txt
我从来没有设法使“\t”元字符与grep一起工作。 然而,我发现了两个替代方案:
使用<Ctrl-V> <TAB>(按Ctrl-V然后输入TAB) 使用awk: foo | awk '/\t/'
使用'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
编辑:显然,上面的内容只对查看文件内容以定位选项卡有用——如果目标是将选项卡作为更大的脚本会话的一部分来处理,那么这没有任何有用的用途。
你可能想使用grep "$(echo -e '\t')"
唯一的要求是echo能够解释反斜杠转义。
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').
推荐文章
- 在Windows中有像GREP这样的模式匹配实用程序吗?
- 如何从命令行将每两行合并为一行?
- 如何从命令行通过mysql运行一个查询?
- 在创建守护进程时执行双fork的原因是什么?
- 匹配前后的Grep字符?
- (grep)正则表达式匹配非ascii字符?
- 如何从另一个文件A中删除文件B中出现的行?
- 对以制表符分隔的文件进行排序
- 如何使用查找命令从列表中查找所有具有扩展名的文件?
- 如何将文件指针(file * fp)转换为文件描述符(int fd)?
- 在Bash中获取日期(比当前时间早一天)
- Linux: kill后台任务
- 在OSX中永久设置PATH环境变量
- PowerShell等价于grep -f
- 如何在C程序中获取当前目录?