Unix / Linux: Include Header in Grep Result

Objective: Always include the header line when using grep. In other words, do not perform pattern matching on the header, but only perform pattern matching on the body (from line 2 onwards).

Before looking at the solution, let’s take a closer look at the problem. Let’s try to grep processes with the name “ssh” in them.

As you can see from the above output, the processes that matched the name “ssh” are printed out, but the header row of the ps command is missing. This is because it does not match the pattern “ssh”. The header output of the ps command looks like this.

Now, the problem is how can we include the above header in the grep output. So, our desired output has to be something like this – with the header on top.

The way I normally accomplish it is by using the following syntax.

The output of ps is sent to a list of commands via command grouping. Within command grouping, the first line from the output of ps is read by the read command and the line is printed by the echo command. Subsequent output lines from ps is fed to the grep command.

A better approach is to define the grouped commands in a shell function instead. Define the following grepb function in your shell startup or profile file.

To start using the grepb function, either logout and login to the shell again or source the startup file. This will allow the current shell to recognise the function. Once done, use it with the following syntax.

ibrahim = { interested_in(unix, linux, android, open_source, reverse_engineering); coding(c, shell, php, python, java, javascript, nodejs, react); plays_on(xbox, ps4); linux_desktop_user(true); }