2

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

OG Dude
  • 121
  • 2

2 Answers2

4

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>
Zoredache
  • 130,897
  • 41
  • 276
  • 420
  • +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
1

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>
Bryan
  • 7,628
  • 15
  • 69
  • 94