bashgrep

Always include first line in grep


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