I am having some difficulty understanding the correct syntax to enable a line by line processing of a file using a WolframScript. What I would like to do to mimic this simple python pipe in Mathematica. Take a python file myfunction.py:
#!/usr/bin/env python
import sys
def myfunction():
for line in sys.stdin:
data = line.strip().split("\t")
print "{d[0]}\t{d[1]}".format(d=data)
myfunction()
What this script is doing is taking a text file with tab delimited columns, split at the tabs and then output the first two entries with a tab between them.
Imagine a text file texfile.txt
col11\tcol12\tcol13\tcol14
col21\tcol22\tcol23\tcol24
col31\tcol32\tcol33\tcol34
In a terminal run
$> cat testfile.txt | ./myfunction.py
which prints the output for each line in the file. So I would like to create a WolframScript file that does the same as myfunction.py and can be used in the same way. I've tried various combinations within the script such as:
#!/usr/local/bin/WolframScript -linewise -print -script
tmp = StringSplit[ReadLine[$ScriptCommandLine], "\t"];
tmp[[1]] <> "\t" <> tmp[[2]]
and
#!/usr/local/bin/WolframScript -linewise -print -script
tmp = StringSplit[ReadLine[$ScriptInputString], "\t"];
tmp[[1]] <> "\t" <> tmp[[2]]
and
#!/usr/local/bin/WolframScript -linewise -print -script
tmp = StringSplit[ReadLine[#], "\t"]&;
tmp[[1]] <> "\t" <> tmp[[2]]
and then
$> cat testfile.txt | ./mywolframscript.m
All of the above fail with errors. I can't help feeling this should be trivial but need a pointer to help with the syntax.
Additional reference
Edit
Having got working syntax I have found another issue. I had assumed that -linewise would enable line by line reading. When I use this on a large file what seems to occur is that WolframScript attempts to read the entire file into memory and then, presumably, process it line by line. As a result the small python code runs well on text files of several hundred MB whereas the wolfram script causing a freeze with CPU usage hitting 100%.
So exactly how does someone write a Wolfram script to read a text file in line by line (analogous to ReadLine
in a notebook) and achieve what can be achieved in python with trivial code?
Edit Dec 17, 2016
Wolfram Tech Support advise that it is not possible to create a Wolfram script that does the same as the python script above.
The current design decision was based on the fact that the WLS can be run through the cloud. Because the cloud evaluations are all done in one shot, all the data needs to be read in before it evaluates. Local running of WLS needs a special case to handle reading continuous input, but that has not been developed yet.
Personally I would think that the ability to read files into memory on the cloud is not infinite. There would be many cases in data science where files may be multiple GB. Of course files can be split and other work around possible by why would you do that when it is trivial in python? More to the point how can Mathematica in 2016 not be able to do things that are trivial in python?
Comments
Post a Comment