[CS-FSLUG] An easy way to remove blank lines?

Jukka jukka.ylonen at gmail.com
Sat Sep 10 02:47:50 CDT 2005


2005/9/10, Chris Brault <groundhog3000 at yahoo.com>:
> Here it is in a python file,
> 
> Let's name the file:  line_remover.py
> 
> #--------------------------------------------------
> #!/usr/bin/python
> 
> import os, os.path
> 
> # First we find the file name from the input
> file_name = sys.argv[1]
> 
> # If the file exist, then we extract the lines from it
> if os.path.isfile(file_name) &&
> os.path.exists(file_name):
>     file_reference = file(file_name, 'r')
>     file_lines = file_reference.readlines()
>     file_reference.close()
>     new_lines = file_lines
> 
> # Run through the lines of the file and erase blank
> ones
> for line in file_lines:
>     if len(line) == 0 ||
> line.findall("^*[\t\n\r\f\v]*$") == []:
>        new_lines.remove(line)
> 
> # Rewrite the file without the blank lines
> file_reference = file(file_name, 'w')
> file_reference.writelines(new_lines)
> file_reference.close()
> 
> 
> #---------------------------------------------------
> 
> There it is in easy to understand, straight forward
> python.
> Not the most optimized code, but easy to read.
> 
> Gabe

Thank you for these tips!
So far I was not very succesfull (probably because my lack of
experience with command-line tools). I have attached an example html
file. These were my results:

JD's link gave me a sed script (sed '/ *#/d; /^ *$/d') for a removal
of blank lines and comments. I run it like this:
sed '/ *#/d; /^ *$/d' index.html > sed.html
Script run without errors, but it did not remove empty lines??

Ed's tip was to search two newlines and replace them with a single
newline with editors serch/replace-function. I tried it with Kate and
KWrite, but even they understand regular expressions, they did not
remove newlines. For some reason they could not find even a single hit
??

Josiah gave me a shell script (grep -v ^$). It runs fine but for it
did not remove empty lines?? Is it possible that "empty" lines in my
example html-file are not really "empty"? Some other "newline"-type
hidden characters?

Chris wrote a python script, but it gave me a following error:
jukkamy at linux:~> python line_remover.py index.html
  File "line_remover.py", line 10
    if os.path.isfile(file_name) &&
                                  ^
SyntaxError: invalid syntax

I tried some changes in line 10, but I do not understand python enough
to fix it.


Blessings,
Jukka
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://ofb.biz/pipermail/christiansource_ofb.biz/attachments/20050910/25bd61fe/attachment.html>


More information about the Christiansource mailing list