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
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 9:32 AM, Martin DeMello
[email protected] wrote:
end
end
I am trying to add say 10 different triggers and add a new line after
that.
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
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 `’
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
filemanipulate.rb:57:in `open'
filemanipulate.rb:57:in `<main>'
/Fox
On Wed, Apr 25, 2012 at 9:54 AM, fox foxmaster [email protected]
wrote:
filemanipulate.rb:57:in open' filemanipulate.rb:57:in
’
Please post the exact code you are using.
Cheers
robert
Hi I will attache the code since it gets format when I paste in paste
bin
http://pastebin.com/X5Cmkg5h
/Fox
Martin DeMello wrote in post #1058254:
you are using
out.puts = “…”
when you want
out.puts “…”
martin
Oooh that was stupid of me 
Thanks for the tip and help Martin
Regards
Fox
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
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 04/25/2012 09:17 AM, fox foxmaster wrote:
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 H.
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,"
Use a proper XML parser/writer, like Nokogiri: http://nokogiri.org/
Lars H. wrote in post #1058261:
On 04/25/2012 09:17 AM, fox foxmaster wrote:
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 H.
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 
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 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 
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.
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?
http://pastebin.com/e3rG2W4e
Regards
Fox
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?
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 Thu, Apr 26, 2012 at 12:49 PM, Lars H.
[email protected] wrote:
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?
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
Lars H. 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
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?
You can use node.add_child(new_node), node.add_next_sibling(new_node)
etc. to insert new nodes into the DOM tree.