将grep结果输出到文本文件,需要更干净的输出 [英] Output grep results to text file, need cleaner output

查看:687
本文介绍了将grep结果输出到文本文件,需要更干净的输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用Grep命令在一组文件中查找搜索字符串时,如何将结果转储到文本文件中?

When using the Grep command to find a search string in a set of files, how do I dump the results to a text file?

还有Grep命令的开关,它提供了更整洁的结果以提高可读性,例如每个条目之间的换行或证明文件名和搜索结果合理的方法?

Also is there a switch for the Grep command that provides cleaner results for better readability, such as a line feed between each entry or a way to justify file names and search results?

例如,一个改变的地方...

For instance, a away to change...

./file/path: first result
./another/file/path: second result
./a/third/file/path/here: third result

./file/path: first result

./another/file/path: second result

./a/third/file/path/here: third result

推荐答案

grep -n "YOUR SEARCH STRING" * > output-file

-n将打印行号,而>将grep结果重定向到输出文件.
如果要清洗"结果,则可以使用管道|对其进行过滤,例如:
grep -n "test" * | grep -v "mytest" > output-file 将匹配具有字符串"test"的所有行,除了匹配字符串"mytest"(即开关-v)的行,并将结果重定向到输出文件.
在此职位上可以找到一些很好的grep提示

The -n will print the line number and the > will redirect grep-results to the output-file.
If you want to "clean" the results you can filter them using pipe | for example:
grep -n "test" * | grep -v "mytest" > output-file will match all the lines that have the string "test" except the lines that match the string "mytest" (that's the switch -v) - and will redirect the result to an output file.
A few good grep-tips can be found on this post

这篇关于将grep结果输出到文本文件,需要更干净的输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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