我想知道我需要编辑哪些文件,它们遵循一个易于使用grep(grep -rnw . -e "text"
)搜索的模式,但它会为每个匹配返回相同文件路径的几倍。
我该如何避免呢?
示例:
./rnaspace_cli.py:41:from rnaspace.core.id_tools import id_tools
./rnaspace_cli.py:42:from rnaspace.core.sequence import sequence
./rnaspace_cli.py:44:from rnaspace.core.trace.event import add_seq_event
./rnaspace_cli.py:45:from rnaspace.core.trace.event import disk_error_event
./rnaspace_on_web:33:from rnaspace.ui.web.controller.rnaspace_controller import rnaspace_controller
期望的输出:
./rnaspace_cli.py:41:from rnaspace.core.id_tools import id_tools
./rnaspace_on_web:33:from rnaspace.ui.web.controller.rnaspace_controller import rnaspace_controller
甚至更好的只是路径和文件:
./rnaspace_cli.py
./rnaspace_on_web
答案 0 :(得分:3)
从grep手册页:
-l, --files-with-matches
Suppress normal output; instead print the name of each input file from which output would normally have
been printed. The scanning will stop on the first match. (-l is specified by POSIX.)
所以,
grep -l 'pattern' files*
将仅显示包含模式的文件名
答案 1 :(得分:1)
这应该有效:
grep -rnw . -e "text" | awk -F: '{print $1}' | uniq
修改说明:
awk -F: '{print $1}'
- 将输出拆分为:
- 符号并仅打印第一部分uniq
- 只显示一次重复的行答案 2 :(得分:0)
在grep中使用-l
选项仅在输出中获取文件名:
grep -lrnw . -e "text"