To elaborate on the accepted answer.
I often need to run some Unix pipes to prepare data that I will then input to Mathematica. I tend to keep the data and notebooks in the same directory, so more common for me would be to use
(* set directory to the notebook directory *)
SetDirectory[NotebookDirectory[]]
(* grab the stdout from the pipe *)
raw = RunProcess[{
"/bin/bash", "--login", "-c",
"cat papers.jsonl | jq 'select (.title | contains(\"Tiger\"))'"
},
"StandardOutput",
ProcessDirectory -> NotebookDirectory[]
]
The original Unix pipe cat papers.jsonl | jq 'select (.title | contains("Tiger"))'
was copied and pasted from the terminal inside an already typed ""
pair. By doing so Mathematica will prompt you if you want to escape any quotes or backslashes (which one does).
The --login
make the bash instance behave like the one I use on the command line. In my case that is important because the program jq
, used to manipulate JSON, is not in a standard location. The file papers.jsonl
, a collection of JSON objects, one per line, is in the same directory as the Mathematica notebook, so the ProcessDirectory
option changes the shell's directory before its execution.
The SetDirectory[NotebookDirectory[]]
line is typically at the top of the notebook.