如果你是喜欢写更多行而不是更少行的类型,你可以试试这个:
echo -n "" > output.txt
while IFS= read -r line_input && IFS= read -r line_file <&3
do
echo "$line_input,$line_file" >> output.txt
done <input.txt 3<file.txt
如果没有,您可以简单地使用@RavinderSingh13 提示;-)
更新
有评论说我提供的解决方案比解决方案慢
paste -d ',' file.txt input.txt
所以我测量了执行时间,得到了以下值:
#!/bin/bash
START=$(date +%s.%N)
paste -d ',' file.txt input.txt > output.txt
END=$(date +%s.%N)
DIFF=$(echo "$END - $START" | bc)
echo "$DIFF";
我跑得最快:
.004961322
| test 1 |.005088686 |
| test 2 |.005036140 |
| test 3 |.005019742 |
| test 4 |.004961322 |
| test 5 |.005030747 |
| test 6 |.004978428 |
接下来我检查了我提供的解决方案
#!/bin/bash
START=$(date +%s.%N)
echo -n "" > output.txt
while IFS= read -r line_input && IFS= read -r line_file <&3
do
echo "$line_input,$line_file" >> output.txt
done <input.txt 3<file.txt
END=$(date +%s.%N)
DIFF=$(echo "$END - $START" | bc)
echo "$DIFF";
我得到了
.002386748
| test 1 |.003516318 |
| test 2 |.003538960 |
| test 3 |.002386748 |
| test 4 |.002584314 |
| test 5 |.003461923 |
| test 6 |.003619142 |
所以我的解决方案有点长,但比使用 paste 更快。
测试在同一系统上运行。