Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

add header to stdout stream in single-line bash pipeline

Tags:

bash

I'm trying to add a header to a stdout stream. The best I could come up with is the following, using a temp file and cat's hyphen option. How can I turn this into a one-liner pipeline? Thanks.

python myscript.py myparm > tmp
echo 'a,b,c' | cat - tmp  > output
like image 537
user2105469 Avatar asked Oct 31 '13 20:10

user2105469


People also ask

How to pipe both stderr and stdout in Bash?

If you want to pipe both the stderr and stdout to the next command, then use the “|&” instead. Now we know how these streams work, let’s have a look at how you can redirect them. Piping is a form of redirection. However, it only involves stdin and stdout. Bash allows specific control over all three of the streams.

What is a pipeline command in Bash?

According to the bash manual section on pipelines (3.2.2 Pipelines), A pipeline is a sequence of one or more commands separated by one of the control operators ‘|’ or ‘|&’. That means every command is a pipeline whether or not you use its pipeline control operators.

How to redirect stdin and stdout in the same command line?

Interestingly, you can redirect both stdin and stdout in the same command line. Here, the following command will use hello.txt as stdin and send the stdout of the command to a file. Redirecting stderr is similar to stdout.

How do I redirect a stdout to a file in Bash?

Piping is a form of redirection. However, it only involves stdin and stdout. Bash allows specific control over all three of the streams. To redirect the stdout content to a file, add the “>” angle followed by the target file name.


1 Answers

{ echo 'a,b,c'; python myscript.py myparm; } > output

No temp file or unnecessary cat needed.

like image 102
John Kugelman Avatar answered Oct 05 '22 23:10

John Kugelman