Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.ruby > #7396 > unrolled thread

editing file in ruby

Started bymohan.mohta@gmail.com
First post2018-07-19 14:49 -0700
Last post2018-07-23 22:41 +0200
Articles 5 — 3 participants

Back to article view | Back to comp.lang.ruby


Contents

  editing file in ruby mohan.mohta@gmail.com - 2018-07-19 14:49 -0700
    Re: editing file in ruby Mohan Mohta <mohan.mohta@gmail.com> - 2018-07-21 08:33 -0700
      Re: editing file in ruby Robert Klemme <shortcutter@googlemail.com> - 2018-07-21 18:25 +0200
        Re: editing file in ruby Mohan Mohta <mohan.mohta@gmail.com> - 2018-07-22 12:51 -0700
          Re: editing file in ruby Robert Klemme <shortcutter@googlemail.com> - 2018-07-23 22:41 +0200

#7396 — editing file in ruby

Frommohan.mohta@gmail.com
Date2018-07-19 14:49 -0700
Subjectediting file in ruby
Message-ID<7df54206-cde1-4dc7-a66c-190f0dc892a8@googlegroups.com>
Hello
I am trying to update /boot/grub/grub.conf
I need to disable huge page on the server and the way to do it is adding the following parameter at the end of the line which specifies which kernel boots up
 transparent_hugepage=never

Can you let me know how can this be done via ruby 

Sample file 

# grub.conf generated by anaconda
#
# Note that you do not have to rerun grub after making changes to this file
# NOTICE:  You have a /boot partition.  This means that
#          all kernel and initrd paths are relative to /boot/, eg.
#          root (hd0,0)
#          kernel /vmlinuz-version ro root=/dev/mapper/rootvg-lv_root
#          initrd /initrd-[generic-]version.img
#boot=/dev/sda
default=0
timeout=5
hiddenmenu
title Red Hat Enterprise Linux Server (2.6.32-696.23.1.el6.x86_64)
        root (hd0,0)
        kernel /vmlinuz-2.6.32-696.23.1.el6.x86_64 ro root=/dev/mapper/rootvg-lv_root nomodeset rd_NO_LUKS LANG=en_US.UTF-8 rd_NO_MD SYSFONT=latarcyrheb-sun16 crashkernel=768M rd_NO_DM  KEYBOARDTYPE=pc KEYTABLE=us rd_LVM_LV=rootvg/lv_root transparent_hugepage=never
        initrd /initramfs-2.6.32-696.23.1.el6.x86_64.img
title Red Hat Enterprise Linux Server (2.6.32-696.18.7.el6.x86_64)
        root (hd0,0)
        kernel /vmlinuz-2.6.32-696.18.7.el6.x86_64 ro root=/dev/mapper/rootvg-lv_root nomodeset rd_NO_LUKS LANG=en_US.UTF-8 rd_NO_MD SYSFONT=latarcyrheb-sun16 crashkernel=768M rd_NO_DM  KEYBOARDTYPE=pc KEYTABLE=us rd_LVM_LV=rootvg/lv_root transparent_hugepage=never
        initrd /initramfs-2.6.32-696.18.7.el6.x86_64.img

[toc] | [next] | [standalone]


#7397

FromMohan Mohta <mohan.mohta@gmail.com>
Date2018-07-21 08:33 -0700
Message-ID<04a7aba2-d2ee-4e59-95d4-462a2994863d@googlegroups.com>
In reply to#7396
Hello
I wrote a program but what beats me is the print works fine and gives me the expected output fir it does not write the same to the file

File.open("/tmp/grub.conf","r").each_line do |line|
        file_boot=File.open("/tmp/bootfile","w")
        data = line.split(" ")
        if data[0] == "kernel"
                data_len=data.length
                if data[data_len-1].strip == " transparent_hugepage=never"
                        file_boot.write(line)
                        print line
                else
                        line1=line.tr("\n","")+" transparent_hugepage=never"+"\n"
                        file_boot.write(line1)
                        print line1
                end
        else
                file_boot.write(line)
                print line
        end
end

Output Printed
===============
# grub.conf generated by anaconda
#
# Note that you do not have to rerun grub after making changes to this file
# NOTICE:  You have a /boot partition.  This means that
#          all kernel and initrd paths are relative to /boot/, eg.
#          root (hd0,0)
#          kernel /vmlinuz-version ro root=/dev/mapper/rootvg-lv_root
#          initrd /initrd-[generic-]version.img
#boot=/dev/sda
default=0
timeout=5
splashimage=(hd0,0)/grub/splash.xpm.gz
hiddenmenu
title Red Hat Enterprise Linux Server (2.6.32-696.23.1.el6.x86_64)
        root (hd0,0)
        kernel /vmlinuz-2.6.32-696.23.1.el6.x86_64 ro root=/dev/mapper/rootvg-lv_root rd_LVM_LV=rootvg/lv_root rd_NO_LUKS rd_NO_MD rd_NO_DM LANG=en_US.UTF-8 SYSFONT=latarcyrheb-sun16 KEYBOARDTYPE=pc KEYTABLE=us crashkernel=auto rhgb quiet elevator=noop transparent_hugepage=never transparent_hugepage=never
        initrd /initramfs-2.6.32-696.23.1.el6.x86_64.img
title Red Hat Enterprise Linux Server (2.6.32-696.18.7.el6.x86_64)
        root (hd0,0)
        kernel /vmlinuz-2.6.32-696.18.7.el6.x86_64 ro root=/dev/mapper/rootvg-lv_root rd_LVM_LV=rootvg/lv_root rd_NO_LUKS rd_NO_MD rd_NO_DM LANG=en_US.UTF-8 SYSFONT=latarcyrheb-sun16 KEYBOARDTYPE=pc KEYTABLE=us crashkernel=auto rhgb quiet elevator=noop transparent_hugepage=never transparent_hugepage=never
        initrd /initramfs-2.6.32-696.18.7.el6.x86_64.img
title Red Hat Enterprise Linux Server (2.6.32-696.3.1.el6.x86_64)
        root (hd0,0)
        kernel /vmlinuz-2.6.32-696.3.1.el6.x86_64 ro root=/dev/mapper/rootvg-lv_root rd_LVM_LV=rootvg/lv_root rd_NO_LUKS rd_NO_MD rd_NO_DM LANG=en_US.UTF-8 SYSFONT=latarcyrheb-sun16 KEYBOARDTYPE=pc KEYTABLE=us crashkernel=auto rhgb quiet elevator=noop transparent_hugepage=never
        initrd /initramfs-2.6.32-696.3.1.el6.x86_64.img


Stored in file
==============

        initrd /initramfs-2.6.32-696.3.1.el6.x86_64.img
t=/dev/mapper/rootvg-lv_root rd_LVM_LV=rootvg/lv_root rd_NO_LUKS rd_NO_MD rd_NO_DM LANG=en_US.UTF-8 SYSFONT=latarcyrheb-sun16 KEYBOARDTYPE=pc KEYTABLE=us crashkernel=auto rhgb quiet elevator=noop transparent_hugepage=never
 transparent_hugepage=never



On Thursday, July 19, 2018 at 4:49:14 PM UTC-5, Mohan Mohta wrote:
> Hello
> I am trying to update /boot/grub/grub.conf
> I need to disable huge page on the server and the way to do it is adding the following parameter at the end of the line which specifies which kernel boots up
>  transparent_hugepage=never
> 
> Can you let me know how can this be done via ruby 
> 
> Sample file 
> 
> # grub.conf generated by anaconda
> #
> # Note that you do not have to rerun grub after making changes to this file
> # NOTICE:  You have a /boot partition.  This means that
> #          all kernel and initrd paths are relative to /boot/, eg.
> #          root (hd0,0)
> #          kernel /vmlinuz-version ro root=/dev/mapper/rootvg-lv_root
> #          initrd /initrd-[generic-]version.img
> #boot=/dev/sda
> default=0
> timeout=5
> hiddenmenu
> title Red Hat Enterprise Linux Server (2.6.32-696.23.1.el6.x86_64)
>         root (hd0,0)
>         kernel /vmlinuz-2.6.32-696.23.1.el6.x86_64 ro root=/dev/mapper/rootvg-lv_root nomodeset rd_NO_LUKS LANG=en_US.UTF-8 rd_NO_MD SYSFONT=latarcyrheb-sun16 crashkernel=768M rd_NO_DM  KEYBOARDTYPE=pc KEYTABLE=us rd_LVM_LV=rootvg/lv_root transparent_hugepage=never
>         initrd /initramfs-2.6.32-696.23.1.el6.x86_64.img
> title Red Hat Enterprise Linux Server (2.6.32-696.18.7.el6.x86_64)
>         root (hd0,0)
>         kernel /vmlinuz-2.6.32-696.18.7.el6.x86_64 ro root=/dev/mapper/rootvg-lv_root nomodeset rd_NO_LUKS LANG=en_US.UTF-8 rd_NO_MD SYSFONT=latarcyrheb-sun16 crashkernel=768M rd_NO_DM  KEYBOARDTYPE=pc KEYTABLE=us rd_LVM_LV=rootvg/lv_root transparent_hugepage=never
>         initrd /initramfs-2.6.32-696.18.7.el6.x86_64.img

[toc] | [prev] | [next] | [standalone]


#7398

FromRobert Klemme <shortcutter@googlemail.com>
Date2018-07-21 18:25 +0200
Message-ID<frh533Fra2tU1@mid.individual.net>
In reply to#7397
On 21.07.2018 17:33, Mohan Mohta wrote:
> Hello
> I wrote a program but what beats me is the print works fine and gives me the expected output fir it does not write the same to the file
> 
> File.open("/tmp/grub.conf","r").each_line do |line|
>          file_boot=File.open("/tmp/bootfile","w")

You are not closing this file properly.

Here's how I'd do it

File.open '/tmp/bootfile', 'w' do |out|
   File.foreach '/tmp/grub.conf' do |line|
     if /^\s+kernel\b/ =~ line
       line.gsub! /transparent_hugepage=\w+/, ''
       line.sub! /\s+$/, ''
       line << ' transparent_hugepage=never'
     end

     out.puts line
   end
end

Cheers

	robert

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

[toc] | [prev] | [next] | [standalone]


#7399

FromMohan Mohta <mohan.mohta@gmail.com>
Date2018-07-22 12:51 -0700
Message-ID<1f224d79-40a4-469a-aa64-57feeb966e01@googlegroups.com>
In reply to#7398
On Saturday, July 21, 2018 at 11:25:15 AM UTC-5, Robert Klemme wrote:
> On 21.07.2018 17:33, Mohan Mohta wrote:
> > Hello
> > I wrote a program but what beats me is the print works fine and gives me the expected output fir it does not write the same to the file
> > 
> > File.open("/tmp/grub.conf","r").each_line do |line|
> >          file_boot=File.open("/tmp/bootfile","w")
> 
> You are not closing this file properly.
> 
> Here's how I'd do it
> 
> File.open '/tmp/bootfile', 'w' do |out|
>    File.foreach '/tmp/grub.conf' do |line|
>      if /^\s+kernel\b/ =~ line
>        line.gsub! /transparent_hugepage=\w+/, ''
>        line.sub! /\s+$/, ''
>        line << ' transparent_hugepage=never'
>      end
> 
>      out.puts line
>    end
> end
> 
> Cheers
> 
> 	robert
> 
> -- 
> remember.guy do |as, often| as.you_can - without end
> http://blog.rubybestpractices.com/

Thanks for your response.
Here is what I did which worked.
The conclusion I drew was when I opened the /tmp/bootfile file and yes the closing as well
so if I opened with before grub.conf it worked 

	file_boot=File.open("/tmp/bootfile","w")
	File.open("/boot/grub/grub.conf","r").each_line do |line|
		data = line.split(" ")
		if data[0] == "kernel"
			data_len=data.length
			tot=data_len-1
			check=0
			(0..tot).each do |cntr|
				if data[cntr].strip == "transparent_hugepage=never"
					file_boot.write(line)
					check=1
				end
			end
			if check == 0
				line1=line.tr("\n","")+" transparent_hugepage=never\n"
				file_boot.write(line1)
			end
		else
			file_boot.write(line)
		end
	end
	file_boot.close

[toc] | [prev] | [next] | [standalone]


#7400

FromRobert Klemme <shortcutter@googlemail.com>
Date2018-07-23 22:41 +0200
Message-ID<frmsrqF64frU1@mid.individual.net>
In reply to#7399
On 22.07.2018 21:51, Mohan Mohta wrote:

> Thanks for your response.

You are welcome.  But I am not sure you understood my point:

> Here is what I did which worked.
> The conclusion I drew was when I opened the /tmp/bootfile file and yes the closing as well
> so if I opened with before grub.conf it worked

The issue is not the order of open operations but that you did not close 
file_boot at all.

> 	file_boot=File.open("/tmp/bootfile","w")
> [...] > 	file_boot.close

You better use the block form of File.open because it is way more robust 
than what you are doing here.

Cheers

	robert


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

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.ruby


csiph-web