bash 将文本附加到命令行的输出

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22122915/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 09:45:57  来源:igfitidea点击:

append text to an output from command line

linuxbash

提问by user3369729

I am looking to run this command

我正在寻找运行此命令

asterisk -rx "core show calls" | grep "active" | cut -d' ' -f1

it will output a number but I want it to append a "0:" at the beginning so the output looks like this

它会输出一个数字,但我希望它在开头附加一个“0:”,所以输出看起来像这样

0:{output from command}

0:{命令输出}

any ideas?

有任何想法吗?

回答by Ignacio Vazquez-Abrams

echo -n "0:" ; asterisk ......

回答by iruvar

roll it all into awk

将其全部放入awk

asterisk -rx "core show calls" | awk '/active/{print "0:"}'

回答by Kostanos

By using sedon the end:

通过使用sed上月底:

asterisk -rx "core show calls" | grep "active" | cut -d' ' -f1 | sed 's/^/0:/g'

by ^ in regular expression you indicate to put 0: in the beginning. You can add any text this way. Also you can add it in any other place inside a string, not only in the beginning.

通过 ^ 在正则表达式中表示将 0: 放在开头。您可以通过这种方式添加任何文本。您也可以将它添加到字符串内的任何其他位置,而不仅仅是在开头。