I found a little tricky way to answer a question.
Is there a more efficient program ?
$ cat SessionLog.m
(* ::Package:: *)
BeginPackage["ProgrammingInMathematica`SessionLog`"]
OpenLog::usage = "filename, opts..] starts logging all input and output to filename."
CloseLog::usage="CloseLog[] closes the logfile opened by OpenLog[]."
Begin["`Private`"]
logfile=""
OpenLog[ filename_String, opts___?OptionQ] := (
logfile = OpenWrite[filename, opts];
outfile=filename;
If[ logfile ===$Failed, Return[logfile] ];
AppendTo[$Echo, logfile];
AppendTo[$Output, logfile];
logfile
)
CloseLog[] := (
$Echo = Complement[$Echo, {logfile}];
$Output = Complement[$Output, {logfile}];
Close[logfile];
(* ----- *)
rawdata=Import[outfile,"Lines"];
dataString= hh2[rawdata];
ofile=(First[#]<>"2."<>Rest[#])&@StringSplit[outfile, "."];
Export[ofile, dataString,"Table"];
(* input *)
in = Select[dataString, StringTake[#, 3] =!= "Out" &];
script=First[StringSplit[outfile, "."]] <> ".wl";
Export[script, Most[in], "Table"];
Print["outfile= ", outfile,", ",ofile,", ",script];
)
(* translate comma to String *)
fcomma[x_]:=If[Length[x]!=0,StringJoin[First[x],MapThread[StringJoin[#1,#2]&,{Table[",",{Length[x]-1}],Rest[x]}]]]
ff[x_]:=
If[StringTake[x[[1]],3]=="Out",
{{fcomma[x]}},Partition[StringSplit[First[StringReplace[{fcomma[x]},"Out["->",Out["]],","],1]]
hh2[y_List]:=If[Head[y[[1]]]===String,Flatten[Apply[Join,ff[#]&/@StringSplit[y,","]]], Flatten[Apply[Join,ff[#]&/@y]]]
End[ ]
Protect[ OpenLog, CloseLog ]
EndPackage[]
$ wolfram
Wolfram Language (Raspberry Pi Pilot Release)
Copyright 1988-2013 Wolfram Research
Information & help: wolfram.com/raspi
In[1]:= << SessionLog.m
In[2]:= OpenLog["math.log"]
Out[2]= OutputStream[math.log, 68]
In[3]:= Expand[ (a+b)^2 ]
2 2
Out[3]= a + 2 a b + b
In[4]:= Factor[ x^5-1 ]
2 3 4
Out[4]= (-1 + x) (1 + x + x + x + x )
In[5]:= CloseLog[]
outfile= math.log, math2.log, math.wl
In[6]:= FilePrint["math.log"]
Out[2]= OutputStream["math.log", 68]
Expand[ (a+b)^2 ]Out[3]= a^2 + 2*a*b + b^2
Factor[ x^5-1 ]Out[4]= (-1 + x)*(1 + x + x^2 + x^3 + x^4)
CloseLog[]
In[7]:= FilePrint["math2.log"]
Out[2]= OutputStream["math.log", 68]
Expand[ (a+b)^2 ]
Out[3]= a^2 + 2*a*b + b^2
Factor[ x^5-1 ]
Out[4]= (-1 + x)*(1 + x + x^2 + x^3 + x^4)
CloseLog[]
In[8]:= FilePrint["math.wl"]
Expand[ (a+b)^2 ]
Factor[ x^5-1 ]
In[9]:= Quit[]
$ wolfram < math.wl
Wolfram Language (Raspberry Pi Pilot Release)
Copyright 1988-2013 Wolfram Research
Information & help: wolfram.com/raspi
In[1]:=
2 2
Out[1]= a + 2 a b + b
In[2]:=
2 3 4
Out[2]= (-1 + x) (1 + x + x + x + x )
In[3]:=
$