0

I need the output of the command below, ie "* master\n remotes/origin/HEAD -> origin/master\n remotes/origin/master\n" to be output for reading.

iex(26)> System.cmd "git", ["-C", "/home/vonhabsi/workpad/Cuis/.git","branch","-a"]  
{"* master\n  remotes/origin/HEAD -> origin/master\n  remotes/origin/master\n",     
 0}  

ie in the form

* master                              
  remotes/origin/HEAD -> origin/master
  remotes/origin/master               

This System.cmd docs adds into: IO.stream(:stdio, :line) to the command

iex(27)> System.cmd "git", ["-C", "/home/vonhabsi/workpad/Cuis/.git","branch","-a"], into: IO.stream(:stdio, :line)
* master
  remotes/origin/HEAD -> origin/master
  remotes/origin/master
{%IO.Stream{device: :standard_io, line_or_bytes: :line, raw: false}, 0}
iex(28)>

What function do I need to take the "* master\n remotes/origin/HEAD -> origin/master\n remotes/origin/master\n" from the tuple and output it as:

* master
  remotes/origin/HEAD -> origin/master
  remotes/origin/master

The additional output {%IO.Stream{device: :standard_io, line_or_bytes: :line, raw: false}, 0} is unwanted. In short how do you take a piece of raw text with \n newlines and output it as it should be printed.

How do you also save it to a file?

vfclists
  • 19,193
  • 21
  • 73
  • 92

1 Answers1

0

There are a couple ways to extract elements from a tuple:

  • pattern matching as @Dogbert suggests
  • elem/2

As for writing to a file, in iex> try h File. and tab to get a list of functions. Then h File.write <cr> to get help.

One possible solution

line = System.cmd(...) |> elem(0)
File.write("path_to_file", line)

If your writing a lot of lines, you may want to open the file first. Look at the help for it with iex> h File.open

Steve Pallen
  • 4,417
  • 16
  • 33