0

What I want is printing the output the first line ( table head ) of ps aux and the grep result.

After search, I come up with following.

ps aux | tee >(head -1 > /dev/tty) | grep mongo

But I find the stdin of grep mongo is cut off.

Also, if I omit > /dev/tty, what will the stdout of head -1 direct to? Why isn't it the console?

(Yes, I know I can achive my purpose by command awk. I am just curious about why my command doesn't work?)

Sisyphus
  • 101
  • 2

1 Answers1

0

You could use awk to match two things at the same time:

  • The first line.
  • The line containing "mongo".

There you go:

$ ps aux | awk 'NR == 1 || /mongo/ {print $0}'
  • The NR == 1 condition matches the first line.
  • The /mongo/ condition matches lines containing "mongo".
  • {print $0} is the action associated with the two previous conditions, in this case: print the whole line.
John WH Smith
  • 15,880