Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pipe grep output to paste input?

Tags:

bash

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.)

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
like image 799
Chylomicron Avatar asked Jul 21 '15 18:07

Chylomicron


People also ask

How do you pipe a grep output?

grep is very often used as a "filter" with other commands. It allows you to filter out useless information from the output of commands. To use grep as a filter, you must pipe the output of the command through grep . The symbol for pipe is " | ".

Does grep print stdout?

Stdout is processed by grep and stderr is printed to the terminal.

How do you pipe the output of a command to a file in Linux?

Conclusion: In Linux, for redirecting output to a file, utilize the ”>” and ”>>” redirection operators or the top command. Redirection allows you to save or redirect the output of a command in another file on your system. You can use it to save the outputs and use them later for different purposes.


3 Answers

You can use process substitution instead of using a temporary file:

paste filename2 <(grep -A 1 LOAD filename1) >> filename3
like image 168
anubhava Avatar answered Sep 19 '22 12:09

anubhava


You can use '-' to represent the input from the pipe

grep -A 1 LOAD filename1.txt | paste filename2.txt - >> filename3.txt
like image 27
biock Avatar answered Sep 18 '22 12:09

biock


grep -A 1 LOAD filename1.txt  | paste filename2.txt /dev/stdin >> filename3.txt  
like image 25
dermen Avatar answered Sep 18 '22 12:09

dermen