BASH:我可以 echo string + grep + sed,但如何在同一行添加更多字符串?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14616041/
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
BASH: i can echo string + grep + sed, but how to add more strings on the same line?
提问by user1348293
Asking a question here is always my last resort. I tried everything even the most embarrassing code so i'm confused on explaining what i tried with no success. I have:
在这里提出问题始终是我的最后手段。我尝试了一切,即使是最令人尴尬的代码,所以我对解释我尝试的内容感到困惑,但没有成功。我有:
echo $output | grep -i -m 1 "Time:" | sed 's/.*\s\([0-9]*:[0-9]*:[0-9]*\).time.*//'
it outputs:
它输出:
23:25:31
Easy.
简单。
But i'd like to add one more string to the end, like " , $year" - so that i have:
但是我想在末尾再添加一个字符串,例如 ", $year" - 这样我就有了:
23:25:31 , 2013
The problem is that whatever i tried (printf, -n, -e, -ne, brackets, quotes, |, ;, &, /r, etc.) gives an error or goes to a new line anyway.
问题是,无论我尝试过什么(printf、-n、-e、-ne、括号、引号、|、;、&、/r 等)都会出错或换行。
Any suggestion will be really appreciated.
任何建议将不胜感激。
Thanks
谢谢
回答by that other guy
time=$(echo $output | grep -i -m 1 "Time:" | sed 's/.*\s\([0-9]*:[0-9]*:[0-9]*\).time.*//')
echo "The time is ${time}, 2013"
回答by anishsane
Alternates
替补
- add
tr -d '\n'at the end of echo+grep+sed pipeline. { entire-echo-grep-sed-pipeline ; echo , 2013 ; } | xargs echo(This however, will add a space before ,)
tr -d '\n'在 echo+grep+sed 管道的末尾添加。{ entire-echo-grep-sed-pipeline ; echo , 2013 ; } | xargs echo(然而,这将在 , 之前添加一个空格)

