我试图创建一个bash脚本来计算文本文件的行和列的平均值和中值。我有平均和中值工作的功能,但是我想格式化行的输出(列格式很好)。如何在两列中的每一列旁边显示两个函数旁边的输出 - Bash脚本
这就是我想要的:
Averages: Medians:
1 1
5 5
7 7
5 6
3 3
6 6
但是我的代码输出目前没有这样的:
Averages: Medians:
1
5
7
5
3
6
1
5
7
6
3
6
我目前拥有的代码是:
if [[ $1 == -r* ]]
then
echo "Averages: Medians"
avg_nums $INPUT_FILE #calls average function on the file
find_median $INPUT_FILE #calls median function
elif...
#block of code for columns which works correctly.
fi
另一件事我我试过了,我的老师建议,将平均值和中位值存入一个临时变量,然后打印当我准备好时,将它们放在一起。它也没有工作。我所做的,这是:
#average function
tempAvg+="$avg" #append the new value of average to a global var
.
.
#median function
tempMed+="${arr[$medIndex]}" #append the new value of median to a global var
,然后调用它下面
if [[ $1 == -r* ]]
then
echo "Averages: Medians"
printf "%s\t%s\n" "$tempAvg" "$tempMed"
elif...
#block of code for columns which works correctly.
fi
赋予的输出:
Averages: Medians:
157536 157636
所以,很显然没有什么我都试过了工作。我对bash脚本也很陌生,只是你知道。有没有人有任何建议如何让这个工作?我是否以这种错误的方式去做?谢谢。
编辑:我的输入文件如下(由制表符分隔)
1 1 1 1 1
9 3 4 5 5
6 7 8 9 7
3 6 8 9 7
3 4 2 1 4
6 4 4 7 7
你的输入文件是什么样的? – JRD