如何使用xargs将输入文件参数重定向至Bash命令?附固定Python命令场景
If you need to dynamically pass input file paths to your fixed Python command (the AllenNLP predict command) using xargs, here are a few straightforward ways to do it, depending on your use case:
Basic Single File Usage
If you have a single file path you want to feed into the command (e.g., from a pipe), you can use xargs to append the input as the final positional argument (which is exactly where the predict command expects the input file):
echo "examples.jsonl" | xargs python -m allennlp.run predict https://s3-us-west-2.amazonaws.com/allennlp/models/ner-model-2018.02.12.tar.gz
This works because xargs takes the input string (your file path) and adds it to the end of your command line, matching the required syntax.
Processing Multiple Input Files
If you have multiple .jsonl files to process one at a time (like all files in a directory), use the -n 1 flag with xargs to run the command once per file:
find . -name "*.jsonl" | xargs -n 1 python -m allennlp.run predict https://s3-us-west-2.amazonaws.com/allennlp/models/ner-model-2018.02.12.tar.gz
The -n 1 ensures xargs passes only one file path per command execution—critical because the predict command expects a single input file at a time.
Using a Placeholder for Custom Argument Order
If you ever need to position the file path somewhere other than the end of the command (though not required here), use the -I flag to define a placeholder for substitution:
echo "examples.jsonl" | xargs -I {} python -m allennlp.run predict https://s3-us-west-2.amazonaws.com/allennlp/models/ner-model-2018.02.12.tar.gz {}
Here, {} gets replaced with the input file path from xargs. This is handy if your command structure changes later and the input file isn't the last argument.
Saving Output to Corresponding Files
To save each predict run's output to a file matching the input name, combine xargs with a shell command wrapper:
find . -name "*.jsonl" | xargs -I {} sh -c 'python -m allennlp.run predict https://s3-us-west-2.amazonaws.com/allennlp/models/ner-model-2018.02.12.tar.gz {} > {}.output'
This will take an input file like examples.jsonl and save its output to examples.jsonl.output. Alternatively, use the predict command's built-in --output-file flag for cleaner syntax:
echo "examples.jsonl" | xargs -I {} python -m allennlp.run predict https://s3-us-west-2.amazonaws.com/allennlp/models/ner-model-2018.02.12.tar.gz {} --output-file {}.output
Important Notes
- If your file paths contain spaces or special characters, use
findwith-print0andxargswith-0to handle them safely:find . -name "*.jsonl" -print0 | xargs -0 -n 1 python -m allennlp.run predict https://s3-us-west-2.amazonaws.com/allennlp/models/ner-model-2018.02.12.tar.gz - Always double-check that
xargsis passing the correct file paths by adding the-tflag (it prints the command before executing it):echo "examples.jsonl" | xargs -t python -m allennlp.run predict https://s3-us-west-2.amazonaws.com/allennlp/models/ner-model-2018.02.12.tar.gz
内容的提问来源于stack exchange,提问作者loretoparisi

