Search code examples
shellunixawksedtext-parsing

How to print only the first non-blank line using sed


Given the following file, where line 1 is blank:

\n
Line 2\n
Line 3\n

How would you output only "Line 2" using sed?

Solutions using other standard UNIX tools, for example awk, are welcome.


Solution

  • The examples work if you have an empty line, but not if you have a line containing characters like space or tab.

    I think this version would work even if the "blank" line contains spaces or tabs.

    sed '/[^[:blank:]]/q;d' file