regex - How to find & replace a pattern with string in ruby? -


my file is:

     [root@test etc]# cat nrpe.cfg
command[check_users]=/usr/local/nagios/libexec/check_users -w 30 -c 35 command[check_load]=/usr/local/nagios/libexec/check_load -w 15,10,5 -c 30,25,20 command[check_disk]=/usr/local/nagios/libexec/check_disk -w 20% -c 10% -p /dev/sda1 command[check_hda]=/usr/local/nagios/libexec/check_disk -w 20% -c 10% -p /dev/sdb command[check_procs]=/usr/local/nagios/libexec/check_procs -w 200 -c 250
want replace /dev/sdb /dev/xvda1 in ruby

first let's write text file:

str =<<bitter_end command[check_users]=/usr/local/nagios/libexec/check_users -w 30 -c 35 command[check_load]=/usr/local/nagios/libexec/check_load -w 15,10,5 -c 30,25,20 command[check_disk]=/usr/local/nagios/libexec/check_disk -w 20% -c 10% -p /dev/sda1 command[check_hda]=/usr/local/nagios/libexec/check_disk -w 20% -c 10% -p /dev/sdb command[check_procs]=/usr/local/nagios/libexec/check_procs -w 200 -c 250 bitter_end  fname_in  = "in" fname_out = "out"  file.write(fname_in, str)   #=> 390 

now read file string, modify string , write output file:

file.write(fname_out, file.read(fname_in).gsub("/dev/sdb", "/dev/xvda1"))   #=> 392 

let's confirm worked:

puts file.read(fname_out) command[check_users]=/usr/local/nagios/libexec/check_users -w 30 -c 35 command[check_load]=/usr/local/nagios/libexec/check_load -w 15,10,5 -c 30,25,20 command[check_disk]=/usr/local/nagios/libexec/check_disk -w 20% -c 10% -p /dev/sda1 command[check_hda]=/usr/local/nagios/libexec/check_disk -w 20% -c 10% -p /dev/xvda1 command[check_procs]=/usr/local/nagios/libexec/check_procs -w 200 -c 250 

Comments

Popular posts from this blog

Ansible - ERROR! the field 'hosts' is required but was not set -

customize file_field button ruby on rails -

SoapUI on windows 10 - high DPI/4K scaling issue -