Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Editing a text file

Reply
Thread Tools

Editing a text file

 
 
barjunk
Guest
Posts: n/a
 
      04-28-2007
There is a task to edit a text file. It is entirely possible that
multiple people are going to be running the ruby script that edits
this file at the same time.

I have looked at Ruby cookbook recipe 6.13 and 20.11. These seem like
what I am wanting.

It seemed like a good idea to float this as a solution before it is
implemented.

How I do it now is using sed:

flock $USERFILE sed -i \
"/^$1[[:space:]]\+/,+6 { s/^\\(.*Default\\)/#\\1/; s/^#\\(.*SRT\\)/\
\1/; }" \
$USERFILE

But I'd like to move this piece of code into the ruby script instead
of launching it with a system command.

What other ways are people doing this sort of thing?

BTW, for those of you that don't have the cookbook, it basically
redefines open to include a flock call.

Mike B.

 
Reply With Quote
 
 
 
 
Robert Klemme
Guest
Posts: n/a
 
      04-28-2007
On 28.04.2007 02:15, barjunk wrote:
> There is a task to edit a text file. It is entirely possible that
> multiple people are going to be running the ruby script that edits
> this file at the same time.
>
> I have looked at Ruby cookbook recipe 6.13 and 20.11. These seem like
> what I am wanting.
>
> It seemed like a good idea to float this as a solution before it is
> implemented.
>
> How I do it now is using sed:
>
> flock $USERFILE sed -i \
> "/^$1[[:space:]]\+/,+6 { s/^\\(.*Default\\)/#\\1/; s/^#\\(.*SRT\\)/\
> \1/; }" \
> $USERFILE
>
> But I'd like to move this piece of code into the ruby script instead
> of launching it with a system command.
>
> What other ways are people doing this sort of thing?
>
> BTW, for those of you that don't have the cookbook, it basically
> redefines open to include a flock call.


I consider Ara the definitive source especially when it comes to
flock'ing NFS mounted files, see for example:

http://blade.nagaokaut.ac.jp/cgi-bin...uby-talk/97211

For the low profile solution check out

http://ruby-doc.org/core/classes/File.html#M002576

Kind regards

robert
 
Reply With Quote
 
 
 
 
barjunk
Guest
Posts: n/a
 
      04-28-2007
<snip old stuff>
> I consider Ara the definitive source especially when it comes to
> flock'ing NFS mounted files, see for example:
>
> http://blade.nagaokaut.ac.jp/cgi-bin...uby-talk/97211
>
> For the low profile solution check out
>
> http://ruby-doc.org/core/classes/File.html#M002576
>
> Kind regards
>
> robert


Thanks Robert.

So if I'm editing this file, the basic idea here is to do this:

require 'lockfile'

Lockfile('lock', :retries => 0) do
# edit my file here
end

And that's it? I'm assuming this will also work on a non-NFS file
system as well. That will do just fine.


Mike B.

 
Reply With Quote
 
Robert Klemme
Guest
Posts: n/a
 
      04-30-2007
On 28.04.2007 20:44, barjunk wrote:
> <snip old stuff>
>> I consider Ara the definitive source especially when it comes to
>> flock'ing NFS mounted files, see for example:
>>
>> http://blade.nagaokaut.ac.jp/cgi-bin...uby-talk/97211
>>
>> For the low profile solution check out
>>
>> http://ruby-doc.org/core/classes/File.html#M002576
>>
>> Kind regards
>>
>> robert

>
> Thanks Robert.
>
> So if I'm editing this file, the basic idea here is to do this:
>
> require 'lockfile'
>
> Lockfile('lock', :retries => 0) do
> # edit my file here
> end
>
> And that's it? I'm assuming this will also work on a non-NFS file
> system as well. That will do just fine.


No, I meant: the basic mechanism uses io.flock, e.g.

11:22:07 [Temp]: cat fl.rb
#!/usr/bin/ruby

id=ARGV.shift || $$

File.open("x", "a") do |io|
puts "#{id}: Start #{Time.now}"
io.flock(File::LOCK_EX)
io.puts "#{id}: #{Time.now.inspect}"
sleep 2
io.puts "#{id}: #{Time.now.inspect}"
puts "#{id}: End #{Time.now}"
end

11:22:12 [Temp]: rm x; for ((i=0;i<10;++i)) do echo $i; ./fl.rb $i &
done; sleep 30
removed `x'
0
[1] 272
1
[2] 2240
2
[3] 3568
3
[4] 2644
4
0: Start Mon Apr 30 11:22:20 +0200 2007
[5] 2688
5
2: Start Mon Apr 30 11:22:20 +0200 2007
1: Start Mon Apr 30 11:22:20 +0200 2007
[6] 2020
6
[7] 3472
7
3: Start Mon Apr 30 11:22:20 +0200 2007
[8] 3332
8
4: Start Mon Apr 30 11:22:20 +0200 2007
[9] 3788
9
[10] 3456
5: Start Mon Apr 30 11:22:20 +0200 2007
6: Start Mon Apr 30 11:22:21 +0200 2007
7: Start Mon Apr 30 11:22:21 +0200 2007
8: Start Mon Apr 30 11:22:21 +0200 2007
9: Start Mon Apr 30 11:22:21 +0200 2007
0: End Mon Apr 30 11:22:22 +0200 2007
2: End Mon Apr 30 11:22:24 +0200 2007
1: End Mon Apr 30 11:22:26 +0200 2007
3: End Mon Apr 30 11:22:28 +0200 2007
4: End Mon Apr 30 11:22:30 +0200 2007
5: End Mon Apr 30 11:22:32 +0200 2007
6: End Mon Apr 30 11:22:34 +0200 2007
7: End Mon Apr 30 11:22:36 +0200 2007
8: End Mon Apr 30 11:22:38 +0200 2007
9: End Mon Apr 30 11:22:40 +0200 2007
[1] Done ./fl.rb $i
[2] Done ./fl.rb $i
[3] Done ./fl.rb $i
[4] Done ./fl.rb $i
[5] Done ./fl.rb $i
[6] Done ./fl.rb $i
[7] Done ./fl.rb $i
[8] Done ./fl.rb $i
[9]- Done ./fl.rb $i
[10]+ Done ./fl.rb $i
11:22:51 [Temp]:

From the timings you can nicely see that exclusive locking works.

robert
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Firefox - Rich Text Editing: Cannot do a text bold...? deostroll Javascript 1 01-11-2008 09:49 PM
Editing particular lines of a text file. Shriphani Python 2 10-09-2007 11:56 AM
Are you looking for high quality photo editing and video editing? gwanglu@gmail.com C Programming 0 07-19-2006 12:32 PM
Opening a text file and editing its contents toomanyjoes@mail.utexas.edu Perl Misc 6 01-16-2005 12:00 PM
Re: editing text file Tim Rowe Python 0 06-25-2003 04:39 PM



Advertisments