如何输出格式正确的原始文本以便在Elixir中读取和归档?

时间:2017-04-22 09:15:43

标签: elixir elixir-iex

我需要输出以下命令,即输出"* master\n remotes/origin/HEAD -> origin/master\n remotes/origin/master\n"进行阅读。

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}  

即表格

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

System.cmd个文档将into: IO.stream(:stdio, :line)添加到命令

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)>

我需要从元组中取出"* master\n remotes/origin/HEAD -> origin/master\n remotes/origin/master\n"并将其输出为:

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

附加输出{%IO.Stream{device: :standard_io, line_or_bytes: :line, raw: false}, 0}是不需要的。 简而言之,如何使用\n换行符创建一段原始文本并输出它应该打印出来。

如何将其保存到文件中?

1 个答案:

答案 0 :(得分:0)

有几种方法可以从元组中提取元素:

  • 模式匹配为@Dogbert建议
  • ELEM / 2

至于写入文件,在iex&gt;尝试h File.和标签以获取功能列表。然后h File.write <cr>获得帮助。

一种可能的解决方案

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

如果你写了很多行,你可能想先打开文件。用iex&gt;查看它的帮助。 h File.open