在bash连接两个命令的输出没有换行符 [英] Concatenate in bash the output of two commands without newline character
问题描述
我需要什么:
假设我有两个命令, A
和 B
,每个返回一个行字符串(即,没有换行符的字符串,除了在最后可能1)。我需要一个命令(或管道命令序列) C
的并置命令的输出 A
和 B
在同一行,并插入它们之间1空格字符。
的应该是如何工作的例子:
例如,假设命令的输出 A
是字符串的的的这里的引号:
快速
和假设命令 B
的输出是字符串的的的引号的位置:
棕色狐狸
然后我想指令(S) C
的输出为字符串的 的之间在这里引号:
敏捷的棕色狐狸
我的最好的尝试性解决方案:
在试图找出 C
由我自己,它似乎是管道命令的后续顺序应该工作:
{回声快速;回声棕色狐狸; } | xargs的-I {}回声{} | SED的/ \\ n //'
不幸的是,此命令的输出是
快速
棕色狐狸
您可以使用 TR
:
{回声快速;回声棕色狐狸; } | TR\\ n
或使用SED:
{回声快速;回声棕色狐狸; } | SED -e'N; S / \\ n / /'
输出:
敏捷的棕色狐狸
What I need:
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.
Example of how it should work:
For example, suppose the output of command A
is the string between the quotation marks here:
"The quick"
And suppose the output of command B
is the string between the quotation marks here:
"brown fox"
Then I want the output of command(s) C
to be the string between the quotation marks here:
"The quick brown fox"
My best attempted solution:
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/\n//'
Unfortunately, the output of this command is
The quick
brown fox
You can use tr
:
{ echo "The quick"; echo "brown fox"; } | tr "\n" " "
OR using sed:
{ echo "The quick"; echo "brown fox"; } | sed -e 'N;s/\n/ /'
OUTPUT:
The quick brown fox
这篇关于在bash连接两个命令的输出没有换行符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!