如何将 grep 搜索的结果通过管道传输到新的 vi 文件中

Lme*_*dza 73 grep vi

我正在使用grep -e Peugeot -e PeuGeot carlist.txtcarlist.txt 搜索并提取一些项目,我认为这grep -e Peugeot -e PeuGeot carlist.txt | vi会为我通过管道,但这就是我得到的:

Vim: Warning: Input is not from a terminal
Vim: Error reading input, exiting...
Vim: preserving files...
Vim: Finished.
Run Code Online (Sandbox Code Playgroud)

Pet*_*zel 126

使用“-”作为参数运行 vi 或 vim 使其从标准输入读取文件以进行编辑。因此:

grep -e Peugeot -e PeuGeot carlist.txt | vi -
Run Code Online (Sandbox Code Playgroud)

会做你需要的。

  • @MD.MohiuddinAhmed,您希望如何在 Vim 中保留 `grep` 的搜索着色(由不可打印的字符组成)?你总是可以在*in* Vim 中进行搜索。 (6认同)
  • 对于任何看到这一点的人,请注意:它期望来自 `stdin`。在某些情况下,你必须从 `stderr` 重定向到 `stdin`,例如:`valgrind my_file.out 2>&1 | vi -` (4认同)

ken*_*orb 16

除了vim -,您还可以使用以下<(command)语法使用进程替换,例如:

vim <(echo This is example.)
vim <(cat /etc/hosts)
Run Code Online (Sandbox Code Playgroud)

也可以看看:


Jos*_* R. 11

您应该使用输出重定向

grep ... > newfile.txt && vi newfile.txt
Run Code Online (Sandbox Code Playgroud)

另外,我认为您grep可以改进:

grep 'Peu[gG]eot' carlist.txt > newfile.txt && vi newfile.txt
Run Code Online (Sandbox Code Playgroud)


mus*_*usa 6

输出重定向也可以这样使用:

vi <(grep ...)
Run Code Online (Sandbox Code Playgroud)

甚至可以重定向多个命令输出,就好像它们被保存在单独的文件中一样:

vim -d <(ls) <(ls -a)
Run Code Online (Sandbox Code Playgroud)