DISQUS

Tech-Recipes: Remove blank lines from a file using grep | UNIX | Tech-Recipes

  • Anonymous · 5 years ago
    From man grep:

    <ul id="quote">The caret ^ and the dollar sign $ are metacharacters that respectively
    match the empty string at the beginning and end of a line.</ul>
    Not "$=beginning of line, ^=end of line"
  • qmchenry · 5 years ago
    Great catch! You are absolutely correct. The recipe has been repaired. Thank you for helping to improve Tech-Recipes.

    Quinn
  • raxxal · 5 years ago
    By the way, if you want to see the $ sign, in vi enter :set list
    also, in cat do the following: cat -vet

    Raxxal
  • Anonymous · 5 years ago
    .. or, quicker, grep . <filename>
  • Luis · 8 months ago
    awsome
  • Anonymous · 1 year ago
    This wasn't working for me using Cygwin, then I realised that the file I was looking at had Windows line endings. There are any number of solutions to this, but the one I chose was to pipe through tr -d "r" before piping through grep. Then it worked like a charm. -- Richie Hindle, richie@entrian.com
  • Anonymous · 1 year ago
    The easiest way to remove blank lines from a file is to use a sed statement. The following syntax removes the blank lines.

    # cat my_file | sed /^$/d
  • Jadu Saikia · 10 months ago
    All ways together, :-)

    $ grep -v '^$' file
    $ grep '.' file
    $ sed '/^$/d' file
    $ sed -n '/^$/!p' file
    $ awk NF file
    $ awk '/./' file

    unstableme.blogspot.com
  • sivapitchai · 3 months ago
    Have you tried the above commans...?
  • bryansenter · 2 months ago
    Include blank lines that aren't really blank (tabs, spaces, CRLF, etc)

    grep -vc '^[[:space:]]*$'