在 bash 中连接两个没有换行符的命令的输出 [英] Concatenate in bash the output of two commands without newline character
问题描述
我需要什么:
假设我有两个命令,A
和 B
,每个命令都返回一个单行字符串(即,一个没有换行符的字符串,除了在最后).我需要一个命令(或管道命令序列)C
将命令 A
和 B
的输出连接到同一行并插入 1 个空格他们之间的性格.
Suppose I have two commands, A
and B
, each of which returns a single-line string (i.e., a string with no newline character, except possibly 1 at the very end). I need a command (or sequence of piped commands) C
that concatenates the output of commands A
and B
on the same line and inserts 1 space character between them.
工作原理示例:
例如,假设命令 A
的输出是这里的引号 之间的字符串:
For example, suppose the output of command A
is the string between the quotation marks here:
"The quick"
假设命令 B
的输出是这里的引号 之间的字符串:
And suppose the output of command B
is the string between the quotation marks here:
"brown fox"
然后我希望命令C
的输出是在引号之间的字符串:
Then I want the output of command(s) C
to be the string between the quotation marks here:
"The quick brown fox"
我尝试过的最佳解决方案:
在尝试自己找出 C
时,似乎以下管道命令序列应该起作用:
In trying to figure out C
by myself, it seemed that the follow sequence of piped commands should work:
{ echo "The quick" ; echo "brown fox" ; } | xargs -I{} echo {} | sed 's/
//'
不幸的是,这个命令的输出是
Unfortunately, the output of this command is
The quick
brown fox
推荐答案
你可以使用tr
:
{ echo "The quick"; echo "brown fox"; } | tr "
" " "
或使用 sed:
{ echo "The quick"; echo "brown fox"; } | sed ':a;N;s/
/ /;ba'
输出:
The quick brown fox
这篇关于在 bash 中连接两个没有换行符的命令的输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!