如何grep包含一些二进制数据的文本文件? [英] How to grep a text file which contains some binary data?

查看:81
本文介绍了如何grep包含一些二进制数据的文本文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

grep 返回

<前>二进制文件 test.log 匹配

例如

echo "line1 re \x00\r\nline2\r\nline3 re\r\n" >test.log # 在 zsh 中echo -e "line1 re \x00\r\nline2\r\nline3 re\r\n" >test.log # 在 bash 中grep重新测试.log

我希望结果会显示 line1 和 line3(共两行).

是否可以使用tr将不可打印的数据转换成可读的数据,让grep重新工作?

解决方案

你可以通过cat -v运行数据文件,例如

$ cat -v tmp/test.log |格雷普雷第 1 行重新 ^@^Mline3 re^M

然后可以进一步后处理以删除垃圾;这与您关于将 tr 用于任务的查询最为相似.

-v 只是告诉 cat 显示非打印字符.

grep returns

Binary file test.log matches

For example

echo    "line1 re \x00\r\nline2\r\nline3 re\r\n" > test.log  # in zsh
echo -e "line1 re \x00\r\nline2\r\nline3 re\r\n" > test.log  # in bash
grep re test.log

I wish the result will show line1 and line3 (total two lines).

Is it possible to use tr convert the unprintable data into readable data, to let grep work again?

解决方案

You could run the data file through cat -v, e.g

$ cat -v tmp/test.log | grep re
line1 re ^@^M
line3 re^M

which could be then further post-processed to remove the junk; this is most analogous to your query about using tr for the task.

-v simply tells cat to display non-printing characters.

这篇关于如何grep包含一些二进制数据的文本文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆