dpzff20644 2019-07-05 04:05
浏览 620

将shell输出转换为float64?

i am using shell with golang to access apache log file and get some data. first i used to write output to the file directly and it was working, but now i need to get the output and use it in the program directly. and also i need to convert it to float64. i tried converting it into a string and then to float64, but it is not working?

func Mem_usage_data(j int) (Mem_predict float64, err error) {
    awkPart := fmt.Sprintf("awk '{print $%d/1024}'", j)

    out1, err := exec.Command("bash", "-c", "tail -n 1 /var/log/apache2/access.log| "+awkPart+" ").Output()
    fmt.Println("memory usage is", out1)
    s1 := string(out1)
    v1, err1 := strconv.ParseFloat(s1, 64)
    if err1 != nil {
        fmt.Println(err)
    }
    if err != nil {
        fmt.Println(err)
    }
    return v1, err
}

when i print the out1 i get something like this [48 46 49 50 48 49 49 55 10]. CAn you please help how to get the exact output in out1 and how to convert it into float64?

  • 写回答

1条回答 默认 最新

  • doudun1029 2019-07-05 04:22
    关注

    The conversion fails probably because there is some whitespace or newline character on s1. Try to trim it first before doing the conversion. Use strings.TrimSpace() to achieve that.

    v1, err1 := strconv.ParseFloat(strings.TrimSpace(s1), 64)
    
    评论

报告相同问题?

悬赏问题

  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘