Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to parse output of external command in Julia?

Tags:

parsing

julia

Let us say that I have an external command called "Busca01.x" which returns three integers separated by tabs, 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 seem to not make it work, despite the obvious structure of the data. I think in readdlm may be the answer, but I cannot seem to make it work.

My Julia is 3.7.pre 22.

like image 628
wpkzz Avatar asked Jan 02 '26 07:01

wpkzz


1 Answers

Since readall returns a String, you want something that operates on a String, split fits the bill.

Base.split(string, [chars]; limit=0, keep=true)

Return an array of substrings by splitting the given string on occurrences of the given character delimiters, which may be specified in any of the formats allowed by "search"'s second argument (i.e. a single character, collection of characters, string, or regular expression). If "chars" is omitted, it defaults to the set of all space characters, and "keep" is taken to be false. The two keyword arguments are optional: they are are a maximum size for the result and a flag determining whether empty fields should be kept in the result.

So given the output of something like

julia> x = readall(pipe(`echo "A B C"`,`awk -F ' ' '{print $1"\t"$2"\t"$3 }'`))
"A\tB\tC\n"

the fields are

julia> split(x)
3-element Array{SubString{ASCIIString},1}:
 "A"
 "B"
 "C"

or make it into a tuple

julia> tuple(split(x)...)
("A","B","C")
like image 78
waTeim Avatar answered Jan 05 '26 05:01

waTeim



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!