'Mathematica : Convert easily a Notebook format into Wolframscript (.wls) by removing all comments on real instructions lines
I have a Mathematica notebook.
I would like to generate in an automatically way this notebook to a Wolfram script that I could make run directly from terminal under MacOS with : $ wolframscript output_wls.wls
The problem is that, if i export directly from Mathematica to .wls, I have all the lines of the code which are commented.
For example, I get from Mathematica Notebook exporting
the following beginning into Wolfram script
:
#!/usr/bin/env wolframscript
(* ::Package:: *)
(* ::Input:: *)
(*SetDirectory[NotebookDirectory[]];*)
(*(*Needs["ErrorBarPlots`"];*)
(*Needs["ComputationalGeometry`"];*)
(*Off[CompiledFunction::cflist];*)*)
(* ::Subchapter:: *)
(*Expansion rate date loading*)
(* ::Input:: *)
(*dataH=Import["H_All.txt","Table"];*)
(*dataH = DeleteCases[dataH, x_?(Length[#]==0&), 1];*)
(*ndata=Length[dataH];*)
(*zLine = dataH[[All, 1]];*)
(* ::Subchapter:: *)
(*BD solver with redshift*)
(* ::Input:: *)
(*RK4Method[dH_,d\[Phi]_,d\[Rho]dm_,du_,\[CapitalOmega]dm_,\[CapitalOmega]k_,H0_,\[Phi]0_,d\[Phi]0_,\[Omega]BD_,zLine_]:=Module[*)
(*{h, Htable, \[Rho]dmtable, \[Phi]table, utable, Hk1, Hk2, Hk3,Hk4, \[Rho]dmk1, \[Rho]dmk2, \[Rho]dmk3,\[Rho]dmk4, \[Phi]k1, \[Phi]k2,\[Phi]k3,\[Phi]k4, uk1,uk2, uk3,uk4, containsIndeterminate, containsComplex, Hval},*)
...
If I execute this .wls
script, nothing happens : that's normal since there are comments everywhere.
So, I would like to fix this by automatically remove the comments for real original commands and keep the others as notebooks comments.
I know that a magic sed
or awk
script or command lines could do the trick but I have not enough background to create a such script. However, I think there may be other alternatives.
If someone could find a way to make automatic this task from any notebook to convert to a working executable Wolfram script, this would be fine to tell it.
UPDATE : I tried the solution of Nathan's method but it fails when I evaluate the notebook. Here below the message :
Solution 1:[1]
Please check if the following command can help
gawk -f convert.awk example.txt
where example.txt contains your example text and convert.awk the following code.
BEGIN {FS = ""}
{
for (i = 1; i <= NF; i++) {
if ((($i == "(") && ($(i+1) == "*")) || (($i == "*") && ($(i+1) == ")"))) {
i = i + 1
} else {
printf("%c", $i)
}
}
printf("\n")
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | rossifr |