bash 如何通过管道 grep 输出粘贴输入?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31546680/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 13:21:25  来源:igfitidea点击:

How to pipe grep output to paste input?

bash

提问by Chylomicron

I basically want to take the line that is under the word "LOAD" in filename1, and make it the second column in a new file, where the first column comes from filename2. (This is being done inside a loop, but I think that's irrelevant.)

我基本上想取filename1中“LOAD”一词下的行,并将其作为新文件中的第二列,其中第一列来自filename2。(这是在循环内完成的,但我认为这无关紧要。)

So if I have

所以如果我有

grep -A 1 LOAD filename1 >> temp
paste filename2 temp >> filename3
rm temp

Is there a way to do that in one command, with no temp file? Something like

有没有办法在没有临时文件的情况下在一个命令中做到这一点?就像是

grep -A 1 LOAD filename1 | paste filename2 "grep output" >> filename3

采纳答案by anubhava

You can use process substitutioninstead of using a temporary file:

您可以使用进程替换而不是使用临时文件:

paste filename2 <(grep -A 1 LOAD filename1) >> filename3

回答by dermen

grep -A 1 LOAD filename1.txt  | paste filename2.txt /dev/stdin >> filename3.txt  

回答by Ken

You can use '-' in command line to represent input from pipe

您可以在命令行中使用“-”来表示来自管道的输入

grep -A 1 LOAD filename1.txt | paste filename2.txt - >> filename3.txt