Friday, July 22, 2022

[SOLVED] Always include first line in grep

Issue

I often grep CSV files with column names on the first line. Therefore, I want the output of grep to always include the first line (to get the column names) as well as any lines matching the grep pattern. What is the best way to do this?


Solution

You could include an alternate pattern match for the one of the column names. If a column was called COL then this would work:

$ grep -E 'COL|pattern' file.csv


Answered By - DigitalRoss
Answer Checked By - Pedro (WPSolving Volunteer)