If if if statment

Hi all,

if I take this code.

File.open("destination.txt", "w") do |out|
  File.foreach("original.txt") do |line|
    out.puts line
    if line =~ /YOUR_REGEX/
      out.puts "THE NEW LINE"
    end
  end
end

it all works fine but if I do it like this

File.open("destination.txt", "w") do |out|
  File.foreach("original.txt") do |line|
    out.puts line
    if line =~ /YOUR_REGEX/
      out.puts "THE NEW LINE"
      if line =~ /YOUR_REGEX12/
       out.puts "THE NEW LINE12"
     end
   end
end
end

It will only trigger on the first if statment.

I am trying to add say 10 different triggers and add a new line after
that.

Regards
/Fox

···

--
Posted via http://www.ruby-forum.com/.

try this:

File.open("destination.txt", "w") do |out|
File.foreach("original.txt") do |line|
   out.puts line
   if line =~ /YOUR_REGEX/
     out.puts "THE NEW LINE"
   elsif line =~ /YOUR_REGEX12/
     out.puts "THE NEW LINE12"
   end
  end
end

[see http://www.howtogeek.com/howto/programming/ruby/ruby-if-else-if-command-syntax/]

martin

···

On Wed, Apr 25, 2012 at 12:17 AM, fox foxmaster <lists@ruby-forum.com> wrote:

Hi all,

if I take this code.

File.open("destination.txt", "w") do |out|
File.foreach("original.txt") do |line|
out.puts line
if line =~ /YOUR_REGEX/
out.puts "THE NEW LINE"
end
end
end

it all works fine but if I do it like this

File.open("destination.txt", "w") do |out|
File.foreach("original.txt") do |line|
out.puts line
if line =~ /YOUR_REGEX/
out.puts "THE NEW LINE"
if line =~ /YOUR_REGEX12/
out.puts "THE NEW LINE12"
end
end
end
end

It will only trigger on the first if statment.

I am trying to add say 10 different triggers and add a new line after
that.

Regards
/Fox

--
Posted via http://www.ruby-forum.com/.

Thanks Martin,

But when I do it with elsif it will take me one line further but then
stop with error
/pemi/Downloads/script/ruby/filemanipulate.rb:57:in `<main>'

filemanipulate.rb:71:in `block (2 levels) in <main>':
undefined method `puts=' for #<File:1234.bak (closed)> (NoMethodError)
        filemanipulate.rb:58:in `foreach'
        filemanipulate.rb:58:in `block in <main>

        filemanipulate.rb:57:in `open'
        filemanipulate.rb:57:in `<main>'

/Fox

···

--
Posted via http://www.ruby-forum.com/.

Hi I will attache the code since it gets format when I paste in paste
bin

http://pastebin.com/X5Cmkg5h

/Fox

···

--
Posted via http://www.ruby-forum.com/.

One more that is nagging me is this

elsif line =~ /<InterfaceGroup Name="BlacklistGroup" Members="/
                    out.puts " << vlan#{$vlan}_#{$Client_name},"

what I want to do here is not to make a new line instead I would like to
insert it or append it to the line "BlacklistGroup" Members= insert
here,"

/Fox

···

--
Posted via http://www.ruby-forum.com/.

Try using a case statement:

File.open("destination.txt", "w") do |out|
   File.foreach("original.txt") do |line|
     out.puts line
     line2 = case line
             when /REGEX_1/ then "NEW_LINE_1"
             when /REGEX_2/ then "NEW_LINE_2"
             when /REGEX_3/ then "NEW_LINE_3"
              end
     out.puts line2 if line2
   end
end

···

On 04/25/2012 09:17 AM, fox foxmaster wrote:

File.open("destination.txt", "w") do |out|
   File.foreach("original.txt") do |line|
     out.puts line
     if line =~ /YOUR_REGEX/
       out.puts "THE NEW LINE"
       if line =~ /YOUR_REGEX12/
        out.puts "THE NEW LINE12"
      end
    end
  end
end

It will only trigger on the first if statment.

I am trying to add say 10 different triggers and add a new line after
that.

--

Lars Haugseth

Lars I thought I check out nokogiri ^^ and humm I feel completely lost,
I am trying to find examples on how to use it in the way that I want, I
really don't get the examples on the nokogiri web page,

So Lars or anyone else, would you like to show me howto use it in the
code I have?

Regards

Fox

···

--
Posted via http://www.ruby-forum.com/.

Another approach

RXS = {
  /YOUR_REGEX/ => "THE NEW LINE",
  /YOUR_REGEX12/ => "THE NEW LINE12",
}

File.open("destination.txt", "w") do |out|
  File.foreach("original.txt") do |line|
    out.puts line

    RXS.each do |rx, add|
      if rx =~ line
        out.puts add
        break # remove this if you want output for all triggers
      end
    end
  end
end

Kind regards

robert

···

On Wed, Apr 25, 2012 at 9:32 AM, Martin DeMello <martindemello@gmail.com> wrote:

try this:

File.open("destination.txt", "w") do |out|
File.foreach("original.txt") do |line|
out.puts line
if line =~ /YOUR_REGEX/
out.puts "THE NEW LINE"
elsif line =~ /YOUR_REGEX12/
out.puts "THE NEW LINE12"
end
end
end

I am trying to add say 10 different triggers and add a new line after
that.

--
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/

Please post the exact code you are using.

Cheers

robert

···

On Wed, Apr 25, 2012 at 9:54 AM, fox foxmaster <lists@ruby-forum.com> wrote:

Thanks Martin,

But when I do it with elsif it will take me one line further but then
stop with error
/pemi/Downloads/script/ruby/filemanipulate.rb:57:in `<main>'

filemanipulate.rb:71:in `block (2 levels) in <main>':
undefined method `puts=' for #<File:1234.bak (closed)> (NoMethodError)
filemanipulate.rb:58:in `foreach'
filemanipulate.rb:58:in `block in <main>

   filemanipulate\.rb:57:in \`open&#39;
   filemanipulate\.rb:57:in \`&lt;main&gt;&#39;

--
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/

you are using

out.puts = "...."

when you want

out.puts "...."

martin

···

On Wed, Apr 25, 2012 at 1:09 AM, fox foxmaster <lists@ruby-forum.com> wrote:

Hi I will attache the code since it gets format when I paste in paste
bin

http://pastebin.com/X5Cmkg5h

/Fox

--
Posted via http://www.ruby-forum.com/.

Also, there are several ways you can clean your code up. Here's one
quick fix: use %{....} as a string delimiter and you won't need to
escape quotes within the string.

martin

···

On Wed, Apr 25, 2012 at 1:09 AM, fox foxmaster <lists@ruby-forum.com> wrote:

Hi I will attache the code since it gets format when I paste in paste
bin

http://pastebin.com/X5Cmkg5h

/Fox

--
Posted via http://www.ruby-forum.com/.

Use a proper XML parser/writer, like Nokogiri: http://nokogiri.org/

···

On 04/25/2012 10:36 AM, fox foxmaster wrote:

One more that is nagging me is this

elsif line =~ /<InterfaceGroup Name="BlacklistGroup" Members="/
                     out.puts "<< vlan#{$vlan}_#{$Client_name},"

what I want to do here is not to make a new line instead I would like to
insert it or append it to the line "BlacklistGroup" Members= insert
here,"

--
Lars Haugseth

Lars Haugseth wrote in post #1058261:

end

It will only trigger on the first if statment.

I am trying to add say 10 different triggers and add a new line after
that.

Try using a case statement:

File.open("destination.txt", "w") do |out|
   File.foreach("original.txt") do |line|
     out.puts line
     line2 = case line
             when /REGEX_1/ then "NEW_LINE_1"
             when /REGEX_2/ then "NEW_LINE_2"
             when /REGEX_3/ then "NEW_LINE_3"
              end
     out.puts line2 if line2
   end
end

--

Lars Haugseth

Hi Lars,

That problem is actually solved but I agree that I need to look in to a
xml parser but not for this projekt :slight_smile:

This is my "new" prob
elsif line =~ /<InterfaceGroup Name="BlacklistGroup" Members="/
                    out.puts " << vlan#{$vlan}_#{$Client_name},"

what I want to do here is not to make a new line instead I would like to
insert it or append it to the line "BlacklistGroup" Members= insert
here,

/Fox

···

On 04/25/2012 09:17 AM, fox foxmaster wrote:

--
Posted via http://www.ruby-forum.com/.

First, you need to parse the source XML into a DOM object:

   require 'nokogiri'

   dom = Nokogiri::XML(File.read source_file)

Then you iterate over all InterfaceGroup nodes and modify attributes:

   dom.xpath("//InterfaceGroup").each do |node|
     node['Members'] = "some value"
   end

Finally, write the modified DOM to the target XML file:

   File.open(target_file, "w") { |f| f.write dom.to_xml }

···

On 04/26/2012 11:23 AM, fox foxmaster wrote:

Lars I thought I check out nokogiri ^^ and humm I feel completely lost,
I am trying to find examples on how to use it in the way that I want, I
really don't get the examples on the nokogiri web page,

So Lars or anyone else, would you like to show me howto use it in the
code I have?

--
Lars Haugseth

Martin DeMello wrote in post #1058254:

you are using

out.puts = "...."

when you want

out.puts "...."

martin

Oooh that was stupid of me :frowning:

Thanks for the tip and help Martin

Regards
Fox

···

--
Posted via http://www.ruby-forum.com/.

Why not? It's evidently the proper tool for the job.

By all means, if you want to use a hammer to drive screws, go right ahead, but you won't get my opinion on the best way to do it.

···

On 04/25/2012 10:49 AM, fox foxmaster wrote:

Hi Lars,

That problem is actually solved but I agree that I need to look in to a
xml parser but not for this projekt :slight_smile:

--
Lars Haugseth

Lars I thought I check out nokogiri ^^ and humm I feel completely lost,
I am trying to find examples on how to use it in the way that I want, I
really don't get the examples on the nokogiri web page,

So Lars or anyone else, would you like to show me howto use it in the
code I have?

Just two nitpicks with regard to file handling:

First, you need to parse the source XML into a DOM object:

require 'nokogiri'

dom = Nokogiri::XML(File.read source_file)

# make Nokogiri read from the file to avoid to copy the whole file into memory
# and read binary!
dom = File.open(source_file, 'rb') {|io| Nokogiri.XML(io)}

Then you iterate over all InterfaceGroup nodes and modify attributes:

dom.xpath("//InterfaceGroup").each do |node|
node['Members'] = "some value"
end

Finally, write the modified DOM to the target XML file:

File.open(target_file, "w") { |f| f.write dom.to_xml }

# again avoid the copy and write binary
File.open(target_file, 'wb') { |io| dom.write_to io }

Kind regards

robert

···

On Thu, Apr 26, 2012 at 12:49 PM, Lars Haugseth <ruby-talk@larshaugseth.com> wrote:

On 04/26/2012 11:23 AM, fox foxmaster wrote:

--
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/

Lars Haugseth wrote in post #1058449:

First, you need to parse the source XML into a DOM object:

   require 'nokogiri'

   dom = Nokogiri::XML(File.read source_file)

Then you iterate over all InterfaceGroup nodes and modify attributes:

   dom.xpath("//InterfaceGroup").each do |node|
     node['Members'] = "some value"
   end

Finally, write the modified DOM to the target XML file:

   File.open(target_file, "w") { |f| f.write dom.to_xml }

Hi Lars and thanks,

When I do this it will insert it like I want but is it also possible to
make a newline like I did with the "old" variant?

Or is it better to first add the lines withe the old variant and then
read that file with nokogiri and insert to the group lines?

Hope I can make myself understood.

Regards
Fox

···

--
Posted via http://www.ruby-forum.com/.

You can use node.add_child(new_node), node.add_next_sibling(new_node)
etc. to insert new nodes into the DOM tree.

···

On 04/27/2012 12:21 PM, fox foxmaster wrote:

When I do this it will insert it like I want but is it also possible to
make a newline like I did with the "old" variant?

--
Lars Haugseth