【问题标题】:Using the output of one command as the argument for another使用一个命令的输出作为另一个命令的参数
【发布时间】:2019-08-26 20:36:27
【问题描述】:

我正在尝试 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】:

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

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

    line=$(grep -nF 'Im looking for this' testfile.txt | cut -f1 -d: )
    vim "+$line" testfile.txt
    

    【讨论】:

    • 我不认为他会这样。在同一个文件上打开多个 vim 实例几乎没有意义。他也没有使用xargs 选项为每个输入单独运行命令。
    • 我认为他使用 xargs 只是因为这是他知道将标准输入转换为参数的唯一方法,而不是因为他期望有多个参数。
    • @Scottt xargs 通常用于将stdin 转换为多个参数。如果它只是一个参数,您通常使用命令替换。
    • 我的意思是var=$(shellcommand)。也可以直接配音到vim命令:vim "+$(grep...)" testfile.txt
    • 顺便说一句,如果你想确保你只得到一个匹配,请使用-m 1
    猜你喜欢
    • 2012-10-25
    • 1970-01-01
    • 1970-01-01
    • 2012-03-27
    • 2021-09-11
    • 2013-11-14
    • 2015-08-18
    • 1970-01-01
    相关资源
    最近更新 更多