0

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?

Bas Peeters
  • 3,269
  • 4
  • 33
  • 49

1 Answers1

1

would this help?

 ps ..|awk '/yourpattern/ ||!/^\s*[0-9]/'

fill yourpattern in place.

also you could do ps ...|awk '/agett[y]/ ||.... instead of /agetty/ to filter the awk process self out.

Kent
  • 189,393
  • 32
  • 233
  • 301