# Finding one certain line in a file is easy but how to look f

**URL:** <https://www.ruby-forum.com/t/finding-one-certain-line-in-a-file-is-easy-but-how-to-look-f/113030>\
**Category:** Ruby\
**Created:** [August 26, 2007, 1:16pm UTC](https://www.ruby-forum.com/t/finding-one-certain-line-in-a-file-is-easy-but-how-to-look-f/113030 "2007-08-26T13:16:27Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![kazaam](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/b4bc9f/32.png) [@kazaam](https://www.ruby-forum.com/u/kazaam)\
**Post date:** [August 26, 2007, 1:16pm UTC](https://www.ruby-forum.com/t/finding-one-certain-line-in-a-file-is-easy-but-how-to-look-f/113030/1 "2007-08-26T13:16:27Z")

</div>

Hi,  
I’m writing a little script for installing ati-drivers on linux machines  
but I have a little problem. I need to change the /etc/X11/xorg.conf  
file for this. This file is built like this (just a cut-out):

Section “InputDevice”  
Identifier “Generic Keyboard”  
Driver “kbd”  
Option “CoreKeyboard”  
Option “XkbRules” “xorg”  
Option “XkbModel” “pc105”  
Option “XkbLayout” “de”  
Option “XkbOptions” “lv3:ralt\_switch”  
EndSection

Section “Device”  
Identifier “Standardgrafikkarte”  
Driver “ati”  
BusID “PCI:1:0:0”  
EndSection

Section “Monitor”  
Identifier “Standardbildschirm”  
Option “DPMS”  
HorizSync 28-64  
VertRefresh 43-60  
EndSection

Section “DRI”  
Mode 0666  
EndSection

Section “Extensions”  
Option “Composite” “Disable”  
EndSection

Now I need to change the driver value within the Section “Device” from  
ati|vesa|radeon|nomatterwhat… to fglrx, but how to find it? E.g.  
IO.foreach(’/etc/X11/xorg.conf’) { |line| puts line if line =~  
/^Section “Device”/ } finds me the correct section but how to “navigate”  
now in it? Is iteration at all the answer to this problem?

greets

---

<div class="post-metadata">

**Author:** ![kazaam](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/b4bc9f/32.png) [@kazaam](https://www.ruby-forum.com/u/kazaam)\
**Post date:** [August 26, 2007, 2:00pm UTC](https://www.ruby-forum.com/t/finding-one-certain-line-in-a-file-is-easy-but-how-to-look-f/113030/2 "2007-08-26T14:00:33Z")

</div>

Hi –

On Sun, 26 Aug 2007, kazaam wrote:

> E.g. IO.foreach(’/etc/X11/xorg.conf’) { |line| puts line if line =~  
> /^Section “Device”/ } finds me the correct section but how to  
> “navigate” now in it? Is iteration at all the answer to this  
> problem?

I usually do something like this (where fh is my filehandle):

while line = fh.gets  
flag = true if /Section “Device”/.match(line)  
flag = false if flag && line.gsub!(/(Driver\s+)"(.\*)"/,  
‘\1"fglrx"’)  
puts line  
end

David

---

<div class="post-metadata">

**Author:** ![kazaam](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/b4bc9f/32.png) [@kazaam](https://www.ruby-forum.com/u/kazaam)\
**Post date:** [August 26, 2007, 2:31pm UTC](https://www.ruby-forum.com/t/finding-one-certain-line-in-a-file-is-easy-but-how-to-look-f/113030/3 "2007-08-26T14:31:40Z")

</div>

kazaam wrote:

> Hi,  
> I’m writing a little script for installing ati-drivers on linux machines  
> but I have a little problem. I need to change the /etc/X11/xorg.conf file  
> for this.

For what you’re trying to do, you could just read the file in as a  
string,  
use String#sub on it and write it to file again, but here is something a  
little more fun (for me to write anyway):  
[http://pastie.caboo.se/91056](http://pastie.caboo.se/91056)  
Usage:  
xconf=XConf.new “/etc/X11/xorg.conf”  
xconf[“Device”][“Driver”]=‘“fglrx”’  
xconf.write #This will backup the old config to /etc/X11/xorg.conf.old  
#or  
xconf.write “new\_config”

The new config won’t contain comments.

I just quickly hacked this up, so this isn’t thoroughly tested and may  
contain  
bugs (the above example works as it should, though, I tested that much).  
There’s obviously some room for improvement here, but for the beginning  
this seems decent enough.

HTH,  
Sebastian

---

<div class="post-metadata">

**Author:** ![kazaam](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/b4bc9f/32.png) [@kazaam](https://www.ruby-forum.com/u/kazaam)\
**Post date:** [August 26, 2007, 5:36pm UTC](https://www.ruby-forum.com/t/finding-one-certain-line-in-a-file-is-easy-but-how-to-look-f/113030/4 "2007-08-26T17:36:06Z")

</div>

thx for your help, I solved it now this way (I know pretty messy) but it  
works fine:

system(‘cp /etc/X11/xorg.conf /etc/X11/xorg.conf.bak’)  
xorgbak = File.open(’/etc/X11/xorg.conf.bak’)  
xorg = File.open(’/etc/X11/xorg.conf’,‘w’)  
drifound = false  
compofound=false  
while line = xorgbak.gets  
driverflag = true if /Section “Device”/.match(line)  
driverflag = false if driverflag && line.gsub!(/(Driver\s+)"(._)"/,  
‘\1"fglrx"’)  
(driflag = true & drifound = true) if /Section “DRI”/.match(line)  
driflag = false if driflag && line.gsub!(/(Mode\s+)._/, ‘\10666’)  
(compoflag = true & compofound = true) if /Section  
“Extensions”/.match(line)  
compoflag = false if compoflag && line.gsub!(/(Option\s+)"(.\*)"/,  
‘\1"Composite" “Disable”’)  
xorg.puts line  
end  
xorgbak.close  
xorg.close  
xorg = File.open(’/etc/X11/xorg.conf’,“a”)  
if not drifound  
xorg.puts’’  
xorg.puts’Section “DRI”’  
xorg.puts’ Mode 0666’  
xorg.puts’EndSection’  
end  
if not compofound  
xorg.puts’’  
xorg.puts’Section “Extensions”’  
xorg.puts’ Option “Composite” “Disable”’  
xorg.puts’EndSection’  
end  
xorg.close
