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.
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
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.
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 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?
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.
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.
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
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,
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 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: