Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

bash: how do I concatenate the output of two commands so that I can pipe them to a third?

$ hg status 

and

$ hg status --ignored 

give very similar outputs. I'd like to concatenate them so I can feed them to awk, as if there were an hg status --all (or svn's svn status --no-ignore)

I'm thinking something like:

$ echo "$(hg status)" "$(hg status --ignored)" | awk  ' ( $1 == "?" ) || ( $1 == "I") { print $2 }' | xargs rm -r 

to make a 'make very clean indeed' command, but it seems to occasionally leave a file behind, perhaps because a newline goes missing or something.

like image 357
John Lawrence Aspden Avatar asked Sep 21 '11 11:09

John Lawrence Aspden


People also ask

How do I combine two Bash commands?

Concatenate Commands With “&&“ The “&&” or AND operator executes the second command only if the preceding command succeeds.

How can you use pipe in multiple commands?

You can make it do so by using the pipe character '|'. Pipe is used to combine two or more commands, and in this, the output of one command acts as input to another command, and this command's output may act as input to the next command and so on.


2 Answers

Use curly braces to group commands:

$ { echo first line; echo second line; } | grep "line" first line second line 

(Posted as an answer from camh's comment)

like image 54
Vincent Scheib Avatar answered Oct 12 '22 00:10

Vincent Scheib


You can use a subshell:

( hg status; hg status --ignored ) | awk '( $1 == "?" ) || ( $1 == "I") { print $2 }' | xargs rm -r 
like image 36
Alan Haggai Alavi Avatar answered Oct 11 '22 23:10

Alan Haggai Alavi