Results 1 to 11 of 11
  1. #1
    Join Date
    Feb 2004
    Posts
    143

    remove blank lines from a file

    i have a data file (.dat) that has 10000 lines out of which 8 lines are blank. what is the command to remove these blank lines from the file?
    Thanks

  2. #2
    Join Date
    Jan 2004
    Location
    Bordeaux, France
    Posts
    320
    Code:
    # Remove empty lines 
    sed '/^$/d' input_file
    
    # Remove empty or blank lines (two methods)
    sed '/^[[:spaces:]]$/' input_line
    awk 'NF>0' input_line
    Jean-Pierre.

  3. #3
    Join Date
    Feb 2004
    Posts
    143
    Thank you very much. It worked.

    Can you also guide me how to find out the total number of
    blank or empty lines in an input file ? Thanks

  4. #4
    Join Date
    Jan 2004
    Location
    Bordeaux, France
    Posts
    320
    Code:
    awk 'NF>0 {count++} END { print count}' input_line
    Jean-Pierre.

  5. #5
    Join Date
    Feb 2004
    Posts
    143
    you are awesome, thanks

  6. #6
    Join Date
    Jun 2002
    Location
    UK
    Posts
    525
    Originally posted by aigles
    Code:
    awk 'NF>0 {count++} END { print count}' input_line
    The above would count the number of non-blank lines. To count the number of blank lines, subtract from the record count.
    Code:
    awk 'NF>0 {count++} END { print NR-count}' yourFile
    Another way would be to use grep -c.
    Code:
    grep -c "^[ \t]*$" yourFile
    Damian

  7. #7
    Join Date
    Jan 2004
    Location
    Bordeaux, France
    Posts
    320
    Oops .... Good shooting Damian !
    Jean-Pierre.

  8. #8
    Join Date
    Jan 2004
    Location
    Bordeaux, France
    Posts
    320
    I forgot to give the correction :
    Code:
    awk 'NF==0 {count++} END { print count}' input_line
    Jean-Pierre.

  9. #9
    Join Date
    Jan 2013
    Posts
    4
    Hi ,

    I used grep -c "^[ \t]*$" File_Name and getting answer as 0 ...

    does that mean there are no lines between records..

    Please tell me how to delete spaces between records from dat file .
    Neha

  10. #10
    Join Date
    Jan 2013
    Posts
    4
    Hi ,

    I used grep -c "^[ \t]*$" File_Name and getting answer as 0 ...

    does that mean there are no lines between records..

    Please tell me how to delete spaces between records from dat file .
    Neha

  11. #11
    Join Date
    Sep 2009
    Location
    Ontario
    Posts
    1,001
    Does the file have newlines in it?
    .dat files are generally created by an application program, and may appear to operating system utilities as a single line, that is, the records are fixed length, and do not have record separators.
    Use 'hexdump' to determine if there are record separators (a line feed is 0a in hexadecimal.)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •