我想写一个shell脚本,其功能如下
> Cat file1和file2,它们具有x的行数和列数(相等的行数和列数).这两个文件都已创建.
>脚本应添加(每个元素的总和)file1和file2中每一行的列元素,并生成输出为file3.
file1-:
10,10,10,10
11,11,11,11
file2-:
5,5,5,5
11,11,11
file3将具有以下输出:
15,15,15,15
22,22,22,22
解决方法:
由于您似乎知道x(即列数),因此可以简单地进行总结.例如,x = 4:
--- script.sh ---
#!/bin/bash
while true; do
read -r c1 c2 c3 c4
read -r d1 d2 d3 d4
if [ -z "$c1" -o -z "$d1" ]; then
break
fi
echo "$(($c1 + $d1)) $(($c2 + $d2)) $(($c3 + $d3)) $(($c4 + $d4))" >>3.txt
done 3<1.txt 4<2.txt
这是一个示例运行:
$./script.sh && cat 1.txt 2.txt 3.txt
1 2 3 4
5 6 7 8
9 9 9 9
1 1 1 1
1 1 1 1
1 1 1 1
2 3 4 5
6 7 8 9
10 10 10 10
标签:shell,linux
来源: https://codeday.me/bug/20191121/2055754.html