How to parse the output of an external command in Julia?
Let's say that I have an external command called "Busca01.x" that returns three tab-separated integers, like this:
karel@maquina: Busca01.x
192 891 9029
So I can call this from julia and store the result as a string using either
readall
or readchomp
. I need the data as an array or tuple, but I don't seem to be working despite the obvious data structure. I think there readdlm
might be an answer, but I can't seem to get it to work.
My Julia is 3.7.pre 22.
source to share
Since readall returns a string, you need something that works on String, split matches count.
Base.split (string, [chars]; limit = 0, keep = true)
Returns an array of substrings, splitting the given string into character data occurrences, which can be specified in any of the formats allowed by searching for the "second" argument (that is, one character, character set, string, or regular expression). If "chars" is omitted, it defaults to all whitespace characters, and "hold" is made to be false. The two keyword arguments are optional: they are the maximum size for the result and a flag that determines whether empty fields should be preserved in the result.
So given the result of something like
julia> x = readall(pipe(`echo "A B C"`,`awk -F ' ' '{print $1"\t"$2"\t"$3 }'`))
"A\tB\tC\n"
fields
julia> split(x)
3-element Array{SubString{ASCIIString},1}:
"A"
"B"
"C"
or turn it into a tuple
julia> tuple(split(x)...)
("A","B","C")
source to share