如何连接标准输入和字符串? [英] How to concatenate stdin and a string?

查看:76
本文介绍了如何连接标准输入和字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何将stdin连接到字符串,像这样?

How to I concatenate stdin to a string, like this?

echo "input" | COMMAND "string"

并获得

inputstring

推荐答案

有点笨拙,但这可能是完成您所问问题的最短方法(使用管道将echo "input"中的stdout接受为stdin转到另一个进程/命令:

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:

echo "input" | awk '{print $1"string"}'

输出:

inputstring

您到底要完成什么任务?更多上下文可以为您提供更好解决方案的更多指导.

What task are you exactly trying to accomplish? More context can get you more direction on a better solution.

更新-回复评论:

@NoamRoss

@NoamRoss

那么,做自己想要的事的更惯用的方法是:

The more idiomatic way of doing what you want is then:

echo 'http://dx.doi.org/'"$(pbpaste)"

$(...)语法称为命令替换.简而言之,它执行包含在新子外壳程序中的命令,并将其stdout输出替换为在父外壳程序中调用$(...)的位置.这样您就可以得到:

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:

echo 'http://dx.doi.org/'"rsif.2012.0125"

这篇关于如何连接标准输入和字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆