bash 如何连接标准输入和字符串?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13884108/
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 concatenate stdin and a string?
提问by Noam Ross
How to I concatenate stdin to a string, like this?
如何将 stdin 连接到一个字符串,像这样?
echo "input" | COMMAND "string"
and get
并得到
inputstring
回答by sampson-chen
A bit hacky, but this might be the shortest way to do what you asked in the question (use a pipe to accept stdout
from echo "input"
as stdin
to another process / command:
有点哈克,但这可能是做你的问题(使用管道来接受询问的最短途径stdout
,从echo "input"
作为stdin
另一个进程/命令:
echo "input" | awk '{print "string"}'
Output:
输出:
inputstring
What task are you exactly trying to accomplish? More context can get you more direction on a better solution.
你到底想完成什么任务?更多的上下文可以为您提供更好的解决方案的更多方向。
Update - responding to comment:
更新 - 回应评论:
@NoamRoss
@诺姆罗斯
The more idiomatic way of doing what you want is then:
做你想做的更惯用的方法是:
echo 'http://dx.doi.org/'"$(pbpaste)"
The $(...)
syntax is called command substitution. In short, it executes the commands enclosed in a new subshell, and substitutes the its stdout
output to where the $(...)
was invoked in the parent shell. So you would get, in effect:
该$(...)
语法称为命令替换。简而言之,它执行包含在新子 shell 中的命令,并将其stdout
输出替换为$(...)
在父 shell 中调用的位置。所以你会得到,实际上:
echo 'http://dx.doi.org/'"rsif.2012.0125"
回答by glenn Hymanman
use cat -
to read from stdin, and put it in $()
to throw away the trailing newline
用于cat -
从标准输入读取,并将其放入$()
以丢弃尾随的换行符
echo input | COMMAND "$(cat -)string"
However why don't you drop the pipe and grab the output of the left side in a command substitution:
但是,为什么不放下管道并在命令替换中获取左侧的输出:
COMMAND "$(echo input)string"
回答by rupert160
I'm often using pipes, so this tends to be an easy way to prefix and suffix stdin:
我经常使用管道,所以这往往是为标准输入添加前缀和后缀的简单方法:
echo -n "my standard in" | cat <(echo -n "prefix... ") - <(echo " ...suffix")
prefix... my standard in ...suffix
回答by Vytenis Bivainis
You can do it with sed:
你可以用 sed 做到:
seq 5 | sed '$a'
seq 5 | sed '$ s/.*/echo input | sed 's/.*/echo input | while read line; do echo $line string; done
string/'
6/'
In your example:
在你的例子中:
echo input | sed "s/$/ string/g"
回答by Simone
There are some ways of accomplish this, i personally think the best is:
有一些方法可以做到这一点,我个人认为最好的是:
(echo input_one ;sleep 5; echo input_two ) | while read line; do echo $line string; done
Another can be by substituting "$" (end of line character) with "string" in a sed command:
另一种方法是在 sed 命令中用“字符串”替换“$”(行尾字符):
input_one string
Why i prefer the former? Because it concatenates a string to stdin instantly, for example with the following command:
为什么我更喜欢前者?因为它会立即将字符串连接到 stdin,例如使用以下命令:
input_two string
you get immediatly the first output:
你立即得到第一个输出:
(echo input_one ;sleep 5; echo input_two ) | sed "s/$/ string/g"
and then after 5 seconds you get the other echo:
然后在 5 秒后你得到另一个回声:
input_one string
input_two string
On the other hand using "sed" first it performs all the content of the parenthesis and then it gives it to "sed", so the command
另一方面,使用“sed”首先执行括号的所有内容,然后将其交给“sed”,因此命令
echo "input" | xargs -J "%" echo "%" "string"
will output both the lines
将输出这两行
Adam
Bob
Charlie
after 5 seconds.
5 秒后。
This can be very useful in cases you are performing calls to functions which takes a long time to complete and want to be continuously updated about the output of the function.
如果您正在调用需要很长时间才能完成的函数并希望不断更新函数的输出,这将非常有用。
回答by user2635263
I know this is a few years late, but you can accomplish this with the xargs -J option:
我知道这已经晚了几年,但是您可以使用 xargs -J 选项完成此操作:
cat names | xargs -n 1 -J "%" echo "I like" "%" "because he is nice"
And since it isxargs, you can do this on multiple lines of a file at once. If the file 'names' has three lines, like:
由于它是xargs,您可以一次在文件的多行上执行此操作。如果文件“名称”有三行,例如:
echo input `COMMAND "string"`
You could do:
你可以这样做:
seq -w 0 100 | xargs -I {} echo "string "{}
回答by Max DeLiso
The command you posted would take the string "input" use it as COMMAND's stdin stream, which would not produce the results you are looking for unless COMMAND first printed out the contents of its stdin and then printed out its command line arguments.
您发布的命令会将字符串“input”用作 COMMAND 的 stdin 流,除非 COMMAND 首先打印出其 stdin 的内容,然后打印出其命令行参数,否则它不会产生您正在寻找的结果。
It seems like what you want to do is more close to command substitution.
看起来你想要做的更接近命令替换。
http://www.gnu.org/software/bash/manual/html_node/Command-Substitution.html#Command-Substitution
http://www.gnu.org/software/bash/manual/html_node/Command-Substitution.html#Command-Substitution
With command substitution you can have a commandline like this:
使用命令替换,您可以拥有这样的命令行:
string 000
string 001
string 002
string 003
string 004
This will first evaluate COMMAND with "string" as input, and then expand the results of that commands execution onto a line, replacing what's between the ‘`' characters.
这将首先使用“字符串”作为输入评估 COMMAND,然后将该命令执行的结果扩展到一行,替换 '`' 字符之间的内容。
回答by clarkttfu
cat will be my choice: ls | cat - <(echo new line)
cat 将是我的选择: ls | cat - <(echo new line)
回答by cy8g3n
Also works:
也有效:
##代码##Will generate strings like:
将生成如下字符串:
##代码##...
...