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

Using xargs to redirect input file parameter to a bash command

我有一个 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 参数的正确方法

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

<(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 需要一个文件(即不能简单地从 /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 {}