Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Grep-ing while keeping first line

I am trying to understand this peculiar behavior. Basically, I'm trying to grep an output of a command while still keeping the first line/header. Thanks for the help in advance.

Success Case

ps -ef | { head -1; grep bash; }

Output:

UID        PID  PPID  C STIME TTY          TIME CMD
username  1008     1  0 Jan21 tty1     00:00:00 -bash
username  1173  1008  0 Jan21 tty1     00:00:00 -bash

Failed Case

 ls -tlrh / | { head -1; grep tmp; }

Output:

total 100K

(i.e.: it ignores the /tmp folder)

like image 471
kuronue Avatar asked Jan 10 '23 01:01

kuronue


1 Answers

@Jotne's answer is better, but sometimes you can use grep -E if you know something in the first line, then you can search for that OR the other thing you want like this with the pipe symbol to express the alternation:

ps -ef | grep -E "UID|bash"

Output

UID   PID  PPID   C STIME   TTY           TIME CMD
502   510   509   0  8:01am ttys000    0:00.08 -bash
502 48806   510   0 10:18am ttys000    0:00.00 grep -E UID|bash
like image 112
Mark Setchell Avatar answered Jan 15 '23 22:01

Mark Setchell