The answers given to this similar question won't do, because those solutions read only one line from stdin.

I want to read all available lines, pretty much in the way that standard Unix programs like cat, grep, wc, etc. do.

EDIT: I'd prefer a "pure-Mathematica" solution, if possible.

  • $\begingroup$ You don't have to accept yet. I'll be surprised if there's a better solution, but let's wait a bit anyway. People are more likely to look at a question if it isn't marked as answered. $\endgroup$ – Szabolcs Dec 15 '16 at 16:25
  • $\begingroup$ @Szabolcs: done $\endgroup$ – kjo Dec 15 '16 at 16:26

I am not aware of any pure Mathematica solution. The usual way is to read from the stream

stream = OpenRead["!cat"]

You can do this directly with any read functions, such as Import, Read, ReadString, ReadList, etc. For example,

Import["!cat", "String"]

I spent a considerable amount of time searching for a simpler solution, and while I cannot rule out that it exists, I wasn't able to find it. You can also read from StartProcess["cat"] (it's readable as a stream), but that's basically the same thing.

These solutions do read all lines on OS X. wolframscript (new in 11) can read from stdin without extra help, but it's buggy: it only reads the data available in the buffer, and then it exists. It doesn't wait for new data like cat does.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.