tags:

views:

45

answers:

2

I have the following code:

File.open("/log/#{hostname}-#{@tdate}-errors.txt",'w') do |o|
  run=tn.cmd('String'=>'sh int', 'Match'=>/#/) { |c| puts c}
  run.each_line do |re|
    title = re.match /([\S]+)Ethernet\S+/
    rep = re.match /\d+ input errors/
    #o.puts run
    o.puts title
    o.puts rep 
   end
 end
 tn.close

It writes to a file just fine, but adds a bunch of spaces between title and rep. How do I get rid of the spaces?

A: 

You can try rstrip to remove trailing whitespace http://ruby-doc.org/core/classes/String.html#M000822

deadkarma
not sure how to use rstrip with the code I have above. Any suggestions?
nomoreflash
The file looks like this: GigabitEthernet3/1 0 input errors with multipe blank lines between GigabitEthernet3/1 and 0 input errors
nomoreflash
`puts` will append a newline at the end of the string. I would recommend doing something like `o.print title.rstrip + rep.rstrip`
deadkarma
A: 

I think you really want to do something like this:

File.open("/log/#{hostname}-#{@tdate}-errors.txt",'w') do |o|
  run = tn.cmd('String'=>'sh int', 'Match'=>/#/) { |c| puts c}
  title = nil
  error_count = nil
  run.each_line do |line|
    title = $1 if line =~/(\S+Ethernet\S+)/
    error_count = Integer($1) if line =~ /(\d+) input errors/
  end
  title ||= 'no title'
  error_count ||= 0
  o.puts title
  o.puts error_count
end
tn.close
Richard Cook
The file looks like this: GigabitEthernet3/10 input errors with multipe blank lines between GigabitEthernet3/1 and 0 input errors
nomoreflash