head command - find first line of non-whitespace

84 views Asked by At

Is there a way to use the head command to find the first line of a file that contains non-whitespace? It would be too kludgy just read the first 100 lines and hope that one of them has non-whitespace.

It doesn't have to be with the head command - all I am looking for is a very performant way to avoid reading the whole file and just getting the first line that matches non-whitespace.

1

There are 1 answers

3
Matt Clark On BEST ANSWER

You can do this with simple grep and a regular expression

grep -m 1 -E "\S+" /path/to/file

The \S+ will match any non-whitespace characters, and with -m 1 we will stop after the first match.