0

我需要以下命令的输出,即"* 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换行符的原始文本并将其输出为应该打印的内容。

您如何将其保存到文件中?

4

1 回答 1

0

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

  • @Dogbert 建议的模式匹配
  • 元素/2

至于写入文件,在 iex> tryh File.和 tab 中获取函数列表。然后h File.write <cr>寻求帮助。

一种可能的解决方案

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

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

于 2017-04-22T15:24:59.870 回答