【问题标题】:how to add new line at the end with awk如何用awk在末尾添加新行
【发布时间】:2018-07-05 19:38:21
【问题描述】:

我搜索并尝试了很多不同的方法,但没有一个真正能满足我的需要。 希望之前没有被问过一百万次。

我的 bashrc 中有这个别名:

alias temp='awk '\''{ printf ("%0.1f",$1/1000); }'\'' < /sys/devices/platform/sunxi-i2c.0/i2c-0/0-0034/temp1_input'

它的输出是:

measure@remote ~ $ temp
10.6measure@remote ~ $

我想要实现的是这样的输出:

measure@remote ~ $ temp
10.6
measure@remote ~ $

【问题讨论】:

    标签: bash shell awk printf newline


    【解决方案1】:

    替换

    "%0.1f"
    

    "%0.1f\n"
    

    【讨论】:

      【解决方案2】:

      如何使用 awk 在末尾添加新行的一般答案是:

      $ awk '... END{print "\n"}'  # or print ORS
      

      【讨论】:

      • @EdMorton 我是正确的。我会暂时搁置这个未经编辑的内容,因为在发布之前没有考虑清楚(然后,它添加了一个新行......)。
      【解决方案3】:

      永远不要使用别名来代替命令,尤其是像这样涉及嵌套引号的命令。只需使用.bashrc 中的一个函数来简化它

      get_ratio() {
          awk '{ printf ("%0.1f\n",$1/1000); }' /sys/devices/platform/sunxi-i2c.0/i2c-0/0-0034/temp1_input
      }
      

      从命令行调用它

      measure@remote ~ $ source ~/.bashrc
      measure@remote ~ $ get_ratio
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-04-30
        • 2012-10-02
        • 2015-09-27
        • 2015-09-23
        • 2021-05-05
        • 2012-08-15
        • 2011-01-06
        • 2021-08-09
        相关资源
        最近更新 更多