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

时间:2018-04-16 08:50:45

标签: bash pipe xargs

我有一个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是否有办法直接将输入重定向到此命令而不是回显到文件?从某种程度上看,该命令似乎不支持stdin 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参数的正确方法

2 个答案:

答案 0 :(得分:3)

创建在命令退出时取消分配的临时文件描述符。它适用于大多数程序并替换文件的参数。

<(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 )

答案 1 :(得分:1)

如果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 {}