6

So I have the following code that reads a text file line by line. Each line ends with a return carriage. The code below outputs the following:

define host {
    use             servers
    host_name         ["buffy"]
    alias         ["buffy"]
    address       ["buffy"].mydomain.com
}

How do i get rid of the bracket and quotes?

File.open('hosts','r') do |f1|

while line = f1.gets
    data = line.chomp.split("\n")

        File.open("nagios_hosts.cfg", "a") do |f|
            f.puts "define host {\n";
            f.puts "\tuse             servers\n"
            f.puts "\thost_name       #{data}"
            f.puts "\talias         #{data}"
            f.puts "\taddress       #{data}.mydomain.com"
            f.puts "\t}\n\n"
        end
    end
end
1
  • got it, changed the one line to read .. data = line.strip Commented Dec 22, 2009 at 17:59

3 Answers 3

6

You could Use

#{data[0]}

instead of #{data}

That's because split creates an array of strings.

However, if you really want to simply get the line without the trailing end of line, you can use the strip method.

Sign up to request clarification or add additional context in comments.

4 Comments

hmmm, ok that works. i forgot to mention, the text file only contains "one column" (the host name). Would I still need to use "#data[0]"
is there a better way of doing it? my text file contains the following: server1 server2 server3 server4
Yes, you always get an array when you use split, even if it is an array with only one element. However, I'm not sure you really need it or if strip would be enough.
oh im a dope, oi don't even need to do anything. i removed the data = line part and just printed "line" inside {}
0
#{data[0]}

split returns an array.

Comments

0

If your file is the following

server1
server2

And you want to produce a file like this :

define host {
use             servers
host_name       server1
alias           server1
address         server1.mydomain.com
}

define host {
use             servers
host_name       server2
alias           server2
address         server2.mydomain.com
}

I would do something like :

output = File.open("output", "w")

File.open("hosts").each_line do |line|
   server_name = line.strip
   text = <<EOF
define host {
use servers
host_name #{server_name}
alias #{server_name}
address #{server_name}.mydomain.com
}
EOF
    output << text
end

output.close

2 Comments

one last question, while I prefer your code to mine, how do I make your code look more organized? What I mean is, if I indent "define host," then in the output file, that line is indented. However, I dont want that, want the other lines to be indented. It actually works to my liking in my code, but yea the f.puts is not appealing (more typing).
Then, another solution would be to put all the lines of text to print in an array (something like lines = ["define host {", "use servers", "host_name #{server_name]"] ) and pass the array to puts (output.puts(lines) ) ... normally it should add the newline if I believe ruby-doc.org/core/classes/IO.html#M002252 ...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.