1

I have quite a large csv file (call it file.csv). It looks like this one:

col1,col2,col3,...
1,2,3
1,2,5
...

So after doing something like this cat file.csv | grep "_some_pattern_" I receive only the output values. Is there a way to append the header (the first line) to the output?

It would be very convenient...

Rui F Ribeiro
  • 55,929
  • 26
  • 146
  • 227
St.Antario
  • 273
  • 6
  • 15

1 Answers1

5

you can use head to print the first line and tail to search with grep starting after the header for the pattern.

head -n 1 file.csv && tail -n +2 file.csv | grep "some pattern"
ryanpattison
  • 166
  • 4
  • If you want to dump the output into a single output file (e.g filter an CSV file) `head -n 1 input.csv > output.csv && tail -n +2 input.csv | egrep -i "some pattern" >> output.csv` – Jorge Mendes Feb 25 '21 at 07:39