Search code examples
node.jsfileutf-8fshead

head command - find first line of non-whitespace


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.


Solution

  • 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.