当我grep
用来查找一些我需要的文本时,它会显示包含与给定模式匹配的行。
例如,
# grep -r ".*Linux" *
path0/output.txt:I hope you enjoyed working on Linux.
path1/output1.txt:Welcome to Linux.
path2/output2.txt:I hope you will have fun with Linux.
Run Code Online (Sandbox Code Playgroud)
然后,我想编辑文件path2/output2.txt
,因此,我输入vim path2/output2.txt
.
但是,我认为这不是一种有效的方法。
之后如何复制路径grep
?
这些是使用 Linux 查找所有文件的安全方法(小写和大写)。
xargs -a <(grep -rlZi Linux *) -0 vim
grep -rlZi Linux *| xargs -0 sh -c 'vim "$@" < /dev/tty' vim
grep -rlZi Linux *| xargs -0 vim # you need to run reset after this command
Run Code Online (Sandbox Code Playgroud)
GNU xargs 和 BSD xargs 之间存在差异,因此可能不适用于 MacOSX。
vim `grep -r ".*Linux" * | cut -f1 -d":"`
Run Code Online (Sandbox Code Playgroud)
反引号用于命令替换。|
用于 grep 的管道输出来切割。-d":"
说基于分隔的切割:
。-f1
切割后的第一个字段说。