The output of some programs start with column names (e.g., ps a
):
$ ps a
PID TTY STAT TIME COMMAND
1306 tty1 Ss+ 0:00 /sbin/mingetty /dev/tty1
1318 hvc0 Ss+ 0:00 /sbin/agetty hvc0 38400 vt100-nav
14696 pts/0 Ss 0:00 -bash
15283 pts/0 R+ 0:00 ps a
Now, let's say I want to find the occurances of agetty:
$ ps a | grep agetty
1318 hvc0 Ss+ 0:00 /sbin/agetty hvc0 38400 vt100-nav
15339 pts/0 S+ 0:00 grep agetty
Unless I've seen the beginning of ps a
's output, I don't know what the output of ps a | grep agetty
means.
I could print the first line followed by the matched lines:
$ ps a | head -n1; ps a | grep agetty
PID TTY STAT TIME COMMAND
1318 hvc0 Ss+ 0:00 /sbin/agetty hvc0 38400 vt100-nav
15542 pts/0 S+ 0:00 grep agetty
However, this doesn't solve my problem when I don't know how many header lines there are. Is there a method to get the headers and the matched lines, or some short workaround to achieve this?