Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

On Linux (Debian), how can I prefix the standard output of a command with one or several lines before piping it to a second command?

This is for mailing the output of a command using sendmail like so:

pflogsumm <args> | sendmail <address>

I cannot specify a subject line this way since sendmail extracts header fields from the input it is fed. I thus want to prefix the pflogsumm output using sed.

Thank you.

/David

share|improve this question

2 Answers 2

You can use a subshell. You can send output from anything you want, and it will all go out through the pipe.

(cat /foo ; echo bar ; pflogsumm <args> ) | sendmail <address>
share|improve this answer
    
Thanks for this. –  OG Dude Feb 28 '12 at 19:15
    
+1 You don't need a subshell, you can use grouping: { cat foo; echo bar; pflogsumm args; } | sendmail -- the trailing semicolon and the whitespace around the braces are required –  glenn jackman Feb 28 '12 at 20:33
    
Neat, thank you. –  OG Dude Feb 29 '12 at 10:16

awk will do what you want.

For example

# ps | awk ' { if (NR == 1) printf("Line 1\nLine 2\n"); print; }'
Line 1
Line 2
  PID TTY          TIME CMD
23071 pts/0    00:00:00 bash
25655 pts/0    00:00:00 ps
25656 pts/0    00:00:00 awk

The above prefixes the printf string before your output.

Your example would be

pflogsumm <args> | awk ' { if (NR == 1) printf("Line 1\nLine 2\n"); print; }' | sendmail <address>
share|improve this answer
    
Cool, I'll try that. Thanks. –  OG Dude Feb 28 '12 at 17:19

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.