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
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.
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.
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.
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.
{ echo 'a,b,c'; python myscript.py myparm; } > output
No temp file or unnecessary cat
needed.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With