背景
我将以下命令作为 shell 脚本存储在 execution.sh
cat 输入文件 |三通输出文件 | java程序
我使用./execution.sh &
成功地从input_file读取,将数据存储在单个output_file中,并作为输入发送到java程序。
问题
我想将input_file
中的数据多次输出到output_file
以及java程序。
例如并行读取相同的 input_file
5 次,并将数据发送到单个 output_file
和单个 java 程序
。
编辑
尝试解决方案
execution.sh
{
python2 readLines.py &
python2 readLines.py &
python2 readLines.py &
python2 readLines.py &
python2 readLines.py &
} | tee output_file | java program
readLines.py
with open('inputfile') as f:
for line in f:
print line
我目前正在使用这个,如果有人看到任何问题,例如竞争条件等,请发表评论。
最佳答案
Reading same input_file say 5 times in parallel and send the data to a single output_file and single java program
让我们忽略“并行”部分。写入只能按顺序进行。
( for i in {1..5}; do cat input_file; done ) | tee out_file | java program
或者简而言之
cat input_file{,,,,} | tee out_file | java program
这两个命令连续打印文件 5 次。
如果您确实想并行编写,您可以启动cat
命令作为后台作业:
( for i in {1..5}; do cat input_file & done ) | tee out_file | java program
此方法保证 output_file
包含 input_file
中的所有字节恰好五次,但(当然)是交错的。很有可能不仅是行,而且字节也最终会交错。这是什么意思?
如果您有该文件
abc
xyz
并行打印两次,输出可能会变成
ababcc
xxyz
yz
如果这不困扰你,还请记住,如果字节序列没有出现在该序列中,它们的含义就会丢失/改变,例如 Windows 新行 \r\n
或多字节 unicode 字符。
关于java - 如何多次发球到单个文件和程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42845855/