網頁

2009年11月19日 星期四

使用Ruby修改檔案內容

以下是使用Ruby修改檔案,主要目的是修改sympa(mailing list)中許多目錄(list)中的config檔案內容。
using ruby modify file content.
config的內容中找到"owner_include"區段將"profile normal"替換成"profile privileged"。

<--
Dir.chdir("/usr/local/sympa/expl")
Dir.glob("*-*").each do |x|
if File::directory?(x)
config_file = File.join(x, 'config')
if File.exist?(config_file)
puts config_file
flag = false
File.open(config_file, 'r+') do |f|
out = ""
f.each do |line|
if line =~ /owner_include/
flag = true
elsif line =~ /^$/
flag = false
elsif flag == true
if line =~ /profile normal/
line = "profile privileged\n"
end
end
out << line
end
#write to file
f.pos = 0 #from line 1
f.print out #write
f.truncate(f.pos) #truncate after current position
end
end
end
end
-->

沒有留言: