for file in md*/*; do ./myscript.sh "$file" >> result.out; done
我使用上面的命令打印myscript的输出并将结果追加到result.out。 result.out中的数据可以在下面看到。
---------------------------------------------------
Report for logfile = ABC/a.log
---------------------------------------------------
ErrorCode 0 12.9.13.43
Time_req
2015-03-29 count 1082.000000
mean 159297.302218
std 143091.598683
min 94656.000000
25% 115803.250000
50% 128557.000000
75% 145264.250000
max 2580735.000000
---------------------------------------------------
Report for logfile = def/c.log
---------------------------------------------------
ErrorCode 0 12.9.13.52 1 12.9.13.51 2 12.9.13.46
Time_req
2015-03-28 count 7.820000e+02 771.000000 7.730000e+02
mean 4.167715e+05 320999.997406 4.109359e+05
std 4.000224e+06 1204153.141004 4.005233e+06
min 8.716000e+04 87598.000000 9.115800e+04
25% 1.104602e+05 109813.000000 1.092160e+05
50% 1.421680e+05 139038.000000 1.406030e+05
75% 2.459625e+05 229918.000000 2.272990e+05
max 1.097561e+08 25290018.000000 1.097695e+08
但我需要水平而不是垂直追加(如横向模式)。我想避免先写临时文件,然后粘贴所有文件。我认为这是一个常见的用例,应该有一个更好的解决方案。这些输出是由python打印的pandas数据帧。
答案 0 :(得分:0)
您可以使用数组元素或关联数组成员作为存储,而不是文件。它们存储在内存中,当然,只能用于合理的小内容。
E.g。你可以:
declare -a res
for file in md*/*; do
res+=( "$(./myscript.sh "$file")" ) #each result as an element of array
done
#here you have the results stored as an array elements
#the $final variable will store the whole output
final=("${res[@]:1}") #store the 1st elemnt to "final" and remove it from the array
# for all remaining elements
# use the "pr" utility for the append up the $width width
width=$(tput cols) #maybe you will need define much larger width...
for ele in "${res[@]}"; do
final=$(pr -w$width -t -m <(echo "$final") <(echo "$ele"))
done
echo "$final"
最后,我不明白
我想避免先写临时文件,然后粘贴所有文件。
使用临时文件比使用内存数组解决方案更容易,更简单。请阅读@Leon评论!