How can I output raw text properly formatted for reading and filing?

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?

To take the first element from a tuple use elem/2 or pattern match it, so you can do any of these:

# Choice 1
iex> {output,0} = System.cmd("git", ["-C", "/home/vonhabsi/workpad/Cuis/.git","branch","-a"])

# Choice 2
iex> blah = System.cmd("git", ["-C", "/home/vonhabsi/workpad/Cuis/.git","branch","-a"])
iex> {output, 0} = blah

# Choice 3
iex> output = System.cmd("git", ["-C", "/home/vonhabsi/workpad/Cuis/.git","branch","-a"])
...>   |> elem(0)

# Choice 4
iex> blah = System.cmd("git", ["-C", "/home/vonhabsi/workpad/Cuis/.git","branch","-a"])
iex> output = elem(blah, 0)

And of course others too, I prefer the pattern matching in this case.

As for the \n those ‘are’ newline, but since you are inspecting it in iex (by it being printed) you see them escaped. To do them as normal just put it on stdout or whatever:

iex> IO.puts(output)
* master
  remotes/origin/HEAD -> origin/master
  remotes/origin/master

:ok

To save it to a file just write the output to the file, nothing special there, just like the puts but with a file. :slight_smile: