使用一个命令的输出作为另一个命令的参数

问题描述 投票:1回答:1

我正在尝试grep一个文件并将行号输出到

vim +{lineNumber} filetoedit

不幸的是,Vim发出错误说

Vim:警告:输入不是来自终端

一个例子:

grep -nF 'Im looking for this' testfile.txt | cut -f1 -d: | xargs vim +{} testfile.tx
bash pipe xargs
1个回答
3
投票

xargs运行的命令从stdin继承xargs,因此它的输入连接到cut的管道,而不是终端。

将结果分配给变量并使用它。

line=$(grep -nF 'Im looking for this' testfile.txt | cut -f1 -d: )
vim "+$line" testfile.txt
© www.soinside.com 2019 - 2024. All rights reserved.