user2105469
user2105469

Reputation: 1433

add header to stdout stream in single-line bash pipeline

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

Upvotes: 5

Views: 2751

Answers (4)

John Kugelman
John Kugelman

Reputation: 361849

No temp file or unnecessary cat needed:

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

Note that both the spaces around the curly braces and the trailing ; are required.

Upvotes: 12

Thiago Mata
Thiago Mata

Reputation: 2959

None of the solutions above worked for my case. But, something similar to the accepted solution worked:

(echo "a,b,c" && my-command)

for example:

(echo "a,b,c" && seq 3) | tee new.log
cat new.log                          
a,b,c
1
2
3

Upvotes: -1

technosaurus
technosaurus

Reputation: 7812

This is the method I use to pipe data through gzip in cgi scripts. Everything in the while loop goes through the output unless you specifically redirect it inside the loop (This can be pretty useful).

RUNONCE=""
while ([ ! "$RUNONCE" ]) do
  RUNONCE=true
  echo stuff
  exec stuff
  cat stuff
  #whatever you want
done >output

Upvotes: -1

hek2mgl
hek2mgl

Reputation: 158070

You can use process substitution:

echo 'a,b,c' | cat - <(python myscript.py myparm)  > output

or:

cat <(echo 'a,b,c') <(python myscript.py myparm)  > output

Upvotes: 6

Related Questions