代码之家  ›  专栏  ›  技术社区  ›  loretoparisi

使用xargs将输入文件参数重定向到bash命令

  •  1
  • loretoparisi  · 技术社区  · 8 年前

    我有一个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 顺便说一句,来自stdin,因此以下将不起作用:

    $ 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 参数

    2 回复  |  直到 8 年前
        1
  •  3
  •   mncl    8 年前

    创建临时文件描述符,该描述符在命令退出时释放。它将在大多数程序上工作,并替换文件的参数。

    <(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 )
    
        2
  •  1
  •   Ole Tange    8 年前

    如果 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 {}
    
    推荐文章