0

for file in md*/*; do ./myscript.sh "$file" >> result.out; done I use the above command to print output from myscript and append result to result.out. Data in result.out can be seen below.

---------------------------------------------------
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

But I need to append horizontally and not vertically (like landscape mode). I want to avoid first write temporary file and then paste all files. I assume this is a common use-case and should have a better solution. These outputs are pandas dataframe printed by python.

pythonRcpp
  • 2,042
  • 6
  • 26
  • 48
  • Possible duplicate of [Display two files side by side](http://stackoverflow.com/questions/13341832/display-two-files-side-by-side) – Leon Mar 30 '17 at 07:44
  • Thanks @Leon, how do I make pr inderstand that 1 input is the stdout and 1 result.out file which intially starts as empty. I tried `for file in md*/*; do ./myscript.sh "$file" | pr -m -t result.out > result.out; done` but doesnt work. – pythonRcpp Mar 30 '17 at 07:51
  • 1
    Use the default Unix convention (a single dash symbol, when used where a filename is expected, stands for stdin). And don't redirect the output to a file that is also used as input (it will be truncated before its contents is consumed by the reading process). – Leon Mar 30 '17 at 07:56
  • Besides you must provide the `-w` option to `pr` (otherwise it will wrap at 72 columns). – Leon Mar 30 '17 at 07:57
  • 1
    So your command should be the following (assuming that the file `result.out` exists): `for file in md*/*; do ./myscript.sh "$file" | pr -m -t -w 10000 result.out - > result.out.tmp; mv result.out.tmp result.out; done` – Leon Mar 30 '17 at 07:58

1 Answers1

0

You can use array elements or associative array members as storage, instead of files. They're stored in the memory, so of course, usable only for reasonable small content.

E.g. you can:

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"

finally, i do not understand the

I want to avoid first write temporary file and then paste all files.

It is much easier and simpler use temp-files as this in-memory array solution. Just read @Leon comments!

clt60
  • 62,119
  • 17
  • 107
  • 194