bash 如何使用awk在末尾添加新行
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/48461783/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to add new line at the end with awk
提问by fluffypuffy
I was searching and trying a lot of different approaches, but non of them really did what I need. Hopefully it was not asked million times before.
我在寻找并尝试了很多不同的方法,但没有一个真正满足我的需求。希望之前没有被问过百万次。
I have this alias in my bashrc:
我的 bashrc 中有这个别名:
alias temp='awk '\''{ printf ("%0.1f",/1000); }'\'' < /sys/devices/platform/sunxi-i2c.0/i2c-0/0-0034/temp1_input'
output of it is:
它的输出是:
measure@remote ~ $ temp
10.6measure@remote ~ $
what I'm trying to achieve is ouput like this:
我想要实现的是这样的输出:
measure@remote ~ $ temp
10.6
measure@remote ~ $
回答by Cyrus
Replace
代替
"%0.1f"
with
和
"%0.1f\n"
回答by Inian
Never use an alias for substituting a command and especially ones involving nested quotes like this. Just use a function in .bashrc
to simplify it
永远不要使用别名来代替命令,尤其是像这样涉及嵌套引号的命令。只需使用一个函数.bashrc
来简化它
get_ratio() {
awk '{ printf ("%0.1f\n",/1000); }' /sys/devices/platform/sunxi-i2c.0/i2c-0/0-0034/temp1_input
}
and call it from the command line
并从命令行调用它
measure@remote ~ $ source ~/.bashrc
measure@remote ~ $ get_ratio
回答by James Brown
Generic answer to how to add new line at the end with awkwould be:
如何使用 awk 在末尾添加新行的通用答案是:
$ awk '... END{print "\n"}' # or print ORS