【问题标题】:Using xargs to redirect input file parameter to a bash command使用 xargs 将输入文件参数重定向到 bash 命令
【发布时间】:2018-09-25 22:47:57
【问题描述】:

我有一个 python 命令(我无法修改),它将文件作为输入并输出结果:

  $ echo '{"sentence": "Did Uriah honestly think he could beat The Legend of Zelda in under three hours?"}' > examples.jsonl
  $ python -m allennlp.run predict https://s3-us-west-2.amazonaws.com/allennlp/models/ner-model-2018.02.12.tar.gz examples.jsonl

用法如下:

usage: python -m allennlp.run [command] predict [-h]
                                                [--output-file OUTPUT_FILE]
                                                [--weights-file WEIGHTS_FILE]
                                                [--batch-size BATCH_SIZE]
                                                [--silent]
                                                [--cuda-device CUDA_DEVICE]
                                                [-o OVERRIDES]
                                                [--include-package INCLUDE_PACKAGE]
                                                [--predictor PREDICTOR]
                                                archive_file input_file

bash 中是否有办法将输入直接重定向到此命令,而不是回显到文件?顺便说一句,该命令似乎不支持来自标准输入的pipe,因此以下将不起作用:

$ echo '{"sentence": "Did Uriah honestly think he could beat The Legend of Zelda in under three hours?"}' | python -m allennlp.run predict https://s3-us-west-2.amazonaws.com/allennlp/models/ner-model-2018.02.12.tar.gz

我尝试过使用xargs,但我不知道处理input_file 参数的正确方法

【问题讨论】:

    标签: bash pipe xargs


    【解决方案1】:

    创建一个临时文件描述符,该描述符在命令退出时被释放。它适用于大多数程序并替换文件的参数。

    <(echo hello)
    

    例子

    grep h <(echo hello)
    
    
    python -m allennlp.run predict https://s3-us-west-2.amazonaws.com/allennlp/models/ner-model-2018.02.12.tar.gz  <( echo '{"sentence": "Did Uriah honestly think he could beat The Legend of Zelda in under three hours?"}' > examples.jsonl )
    

    【讨论】:

    • 该解决方案何时失败?如果allennlp 查找文件,它会失败。如果从bash 调用,那么这也可能有效:echo ... | python -m allennlp... /dev/stdin(但如果allennlp 寻求,这也会失败。
    【解决方案2】:

    如果allennlp 需要一个文件(即不能简单地从 /dev/stdin 读取),那么这将起作用:

    echo '{"sentence": "Did Uriah honestly think he could beat The Legend of Zelda in under three hours?"}' | 
      parallel --pipe -N1 --cat python -m allennlp.run predict https://s3-us-west-2.amazonaws.com/allennlp/models/ner-model-2018.02.12.tar.gz {}
    

    如果您要使用不同的输入运行多个allennlp,它特别有用:

    (echo '{"sentence": "Did Uriah honestly think he could beat The Legend of Zelda in under three hours?"}';
     echo '{"sentence": "its insatiable appetite is tempered by its fear of light."}';
     echo '{"sentence": "You were eaten by a grue"}';) |
      parallel --pipe -N1 --cat python -m allennlp.run predict https://s3-us-west-2.amazonaws.com/allennlp/models/ner-model-2018.02.12.tar.gz {}
    

    【讨论】:

    猜你喜欢
    • 2016-12-14
    • 2014-11-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-07
    • 1970-01-01
    • 2021-02-28
    相关资源
    最近更新 更多