jcr*_*kar 1 unix bash shell sh
我们如何使用多线程将多个文件追加到单个文件中,我的每个文件都有10M的行。所以我想同时处理所有文件?
 #!/bin/bash
appendFiles  A.TXT &
appendFiles  B.TXT &
appendFiles  C.TXT &
wait
function appendFiles 
 {
while  read -r line; do
echo $line >>final.txt
done < $1
} 
您是否尝试过使用以下简单方法cat:
cat A.txt B.txt C.txt > final.txt
这比逐行读取每个文件要快得多,即使是并行完成也是如此。
另外,您也可以尝试并行操作cat,但是对于我的测试,这并不比在一个命令中执行并行操作快。(使用大约1000万行的三个文件进行了测试)
#!/bin/bash
appendFiles  A.TXT &
appendFiles  B.TXT &
appendFiles  C.TXT &
wait
function appendFiles 
{
   cat "$1" >> final.txt
}