# Get rid of extra, blank lines via html parsing?

**URL:** <https://www.ruby-forum.com/t/get-rid-of-extra-blank-lines-via-html-parsing/192691>\
**Category:** Ruby\
**Created:** [August 4, 2010, 6:24am UTC](https://www.ruby-forum.com/t/get-rid-of-extra-blank-lines-via-html-parsing/192691 "2010-08-04T06:24:54Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![mickey](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/ccd318/32.png) [@mickey](https://www.ruby-forum.com/u/mickey)\
**Post date:** [August 4, 2010, 6:24am UTC](https://www.ruby-forum.com/t/get-rid-of-extra-blank-lines-via-html-parsing/192691/1 "2010-08-04T06:24:54Z")

</div>

So I am trying to get some information from a snippet of html  
([\<table class="listing sortable" id="packages\_list"\> \<thead\> - Pastebin.com](http://pastebin.com/iTXyxQ0j)), and im using doc.inner\_text to get the  
important parts, but when I do so I get an odd amount of spacing  
([pino 0.2.11 - Pastebin.com](http://pastebin.com/6HWDs5dm)). is there a way where I can get rid of  
all that extra spacing so I can just print the output and it looks  
clean? possibly something like

pino  
0.2.11-ubuntu0~lucid  
troorl  
(2010-07-04)

pino  
0.2.10-ubuntu0~karmic  
troorl  
(2010-05-27)

that? or can i get each piece of text and add it to an array? if i do  
that while its got all that odd spacing, is that spacing a piece of the  
variable? or is it juts the text?

thanks guys!

---

<div class="post-metadata">

**Author:** ![mickey](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/ccd318/32.png) [@mickey](https://www.ruby-forum.com/u/mickey)\
**Post date:** [August 4, 2010, 8:28am UTC](https://www.ruby-forum.com/t/get-rid-of-extra-blank-lines-via-html-parsing/192691/2 "2010-08-04T08:28:30Z")

</div>

On Wed, Aug 4, 2010 at 6:29 AM, David A. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> (2010-07-04)
> 
> pino  
> 0.2.10-ubuntu0~karmic  
> troorl  
> (2010-05-27)
> 
> that? or can i get each piece of text and add it to an array? if i do  
> that while its got all that odd spacing, is that spacing a piece of the  
> variable? or is it juts the text?

You can remove 2 or more consecutive “\n” like this:

irb(main):001:0\> s =\<\<EOS  
irb(main):002:0" test  
irb(main):003:0"  
irb(main):004:0" test2  
irb(main):005:0" sdfsdf  
irb(main):006:0" werwer  
irb(main):007:0"  
irb(main):008:0"  
irb(main):009:0"  
irb(main):010:0"  
irb(main):011:0" sdfsdfsd  
irb(main):012:0" sdfer234  
irb(main):013:0" EOS  
=\> “test\n\ntest2\nsdfsdf\nwerwer\n\n\n\n\nsdfsdfsd\nsdfer234\n”  
irb(main):019:0\> s.gsub /\n\n+/, “\n”  
=\> “test\ntest2\nsdfsdf\nwerwer\nsdfsdfsd\nsdfer234\n”

or

irb(main):020:0\> s.gsub /\n{2,}/, “\n”  
=\> “test\ntest2\nsdfsdf\nwerwer\nsdfsdfsd\nsdfer234\n”

Hope this helps,

Jesus.

---

<div class="post-metadata">

**Author:** ![mickey](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/ccd318/32.png) [@mickey](https://www.ruby-forum.com/u/mickey)\
**Post date:** [August 4, 2010, 10:47am UTC](https://www.ruby-forum.com/t/get-rid-of-extra-blank-lines-via-html-parsing/192691/3 "2010-08-04T10:47:15Z")

</div>

Use the String methods: s. strip!, s.gsub! and s.squeeze as in  
the following snippet:

# no-white.rb - remove empty lines and sequences of blanks

# from a text file

fh = File.open(‘6HWDs5dm.txt’)  
while( !fh.eof)  
line = fh.readline.chomp  
# remove leading and trailing blanks  
line.strip!  
# skip empty lines  
next if line == ‘’  
# convert tab chars to blanks  
line.gsub!(/\t/,’ ‘)  
# substitute a single blank for a sequence of blanks  
line.squeeze!(’ ')  
# add code to process line if needed  
puts line  
end  
fh.close  
exit(0)

HTH gfb  
“David A.” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote in message  
news:removed\_email\_address@domain.invalid…

---

<div class="post-metadata">

**Author:** ![mickey](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/ccd318/32.png) [@mickey](https://www.ruby-forum.com/u/mickey)\
**Post date:** [August 4, 2010, 4:12pm UTC](https://www.ruby-forum.com/t/get-rid-of-extra-blank-lines-via-html-parsing/192691/4 "2010-08-04T16:12:45Z")

</div>

Hey guys, thanks for the responses. Jesus, the gsubs don’t do anything  
:/, the output still looks the same.

And Gianfranco, everytime I try to use readline, it gives me an error  
“private method `readline’ called for #String:0xb71c3fd8  
(NoMethodError)”

---

<div class="post-metadata">

**Author:** ![mickey](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/ccd318/32.png) [@mickey](https://www.ruby-forum.com/u/mickey)\
**Post date:** [August 4, 2010, 9:29pm UTC](https://www.ruby-forum.com/t/get-rid-of-extra-blank-lines-via-html-parsing/192691/5 "2010-08-04T21:29:01Z")

</div>

On Wed, Aug 4, 2010 at 4:18 PM, David A. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> Hey guys, thanks for the responses. Jesus, the gsubs don’t do anything  
> :/, the output still looks the same.

> And Gianfranco, everytime I try to use readline, it gives me an error  
> “private method `readline’ called for #String:0xb71c3fd8  
> (NoMethodError)”

Can you show your code?

Jesus.
