使用Linux Grep命令

编程入门 行业动态 更新时间:2024-10-11 21:26:36
本文介绍了使用Linux Grep命令 - 需要输出到文本文件,输出更清晰的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

当使用Grep命令在一组文件中查找搜索字符串时,是否可以将结果转储为文本文件? 还有一个Grep命令的开关,它提供更清晰的结果以提高可读性,,比如每个条目之间的换行符? 也许有一个Grep脚本可以输出更清晰的结果

谢谢

解决方案 div>

grep -n您的搜索字符串*>输出文件

-n 会打印行号和> 会将grep-results重定向到输出文件。 如果您想清理结果,您可以使用管道 | 来过滤它们,例如: grep -ntest* | grep -vmytest>输出文件 将匹配所有具有字符串test的行,除了匹配字符串mytest的行(即交换机 -v ) - 并将结果重定向到输出文件。 一些很好的grep-tips可以在上找到这篇文章

When using the Grep command to find a search string in a set of files, is there a way to dump the results to a text file? Also is there a switch for the Grep command that provides cleaner results for better readability, such as a line feed between each entry? Perhaps there is a Grep script that outputs cleaner results

Thanks

解决方案

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

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

更多推荐

使用Linux Grep命令

本文发布于:2023-11-24 06:00:31,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1624174.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:命令   Linux   Grep

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!