[#377882] remove array bracket — Kamarulnizam Rahim <niezam54@...>

Hi when i run my script, the output is as followed:

18 messages 2011/02/02

[#378046] Setter method for Hash value — Rolf Pedersen <rolfhsp@...>

Hi

20 messages 2011/02/03
[#378052] Re: Setter method for Hash value — Brian Candler <b.candler@...> 2011/02/03

Rolf Pedersen wrote in post #979431:

[#378056] Re: Setter method for Hash value — Rolf Pedersen <rolfhsp@...> 2011/02/03

Hi Brian (and others who have contributed with suggestions along the same

[#378144] C extension: How to check if a VALUE is still alive (not being GC'ed)? — Iñaki Baz Castillo <ibc@...>

Hi, I'm coding an async DNS resolver for EventMachine based on udns (a

13 messages 2011/02/05
[#378171] Re: C extension: How to check if a VALUE is still alive (not being GC'ed)? — Tony Arcieri <tony.arcieri@...> 2011/02/06

On Sat, Feb 5, 2011 at 4:02 PM, I=F1aki Baz Castillo <ibc@aliax.net> wrote:

[#378179] Re: C extension: How to check if a VALUE is still alive (not being GC'ed)? — Iñaki Baz Castillo <ibc@...> 2011/02/06

2011/2/6 Tony Arcieri <tony.arcieri@medioh.com>:

[#378199] Choosing an office suite — Hilary Bailey <my77elephants@...>

I am trying to decide which office suite to choose from. The only

30 messages 2011/02/07
[#378229] Re: Choosing an office suite — Phillip Gawlowski <cmdjackryan@...> 2011/02/07

On Mon, Feb 7, 2011 at 11:15 AM, Hilary Bailey <my77elephants@gmail.com> wrote:

[#378202] making hash key from arrays — Arihan Sinha <arihan_sinha@...>

Hi All,

11 messages 2011/02/07

[#378254] "permission denied" happening too often — Peter Bailey <pbailey@...>

Hello,

15 messages 2011/02/08
[#378256] Re: "permission denied" happening too often — Anurag Priyam <anurag08priyam@...> 2011/02/08

> I've got Ruby scripts that have been working fine for years now. But,

[#378257] Re: "permission denied" happening too often — Markus Schirp <mbj@...> 2011/02/08

You can also try to strace your script. In the logs you'll find the system

[#378259] Re: "permission denied" happening too often — Peter Bailey <pbailey@...> 2011/02/08

Markus Schirp wrote in post #980289:

[#378307] undefined class/module YAML::PrivateType - Error — "Priya D." <dharsininitt@...>

Hi,

11 messages 2011/02/09

[#378341] System calls with ` in parameters — "Gerad S." <geradstemke@...>

Hi All,

12 messages 2011/02/09

[#378618] Defining class methods — Tony Arcieri <tony.arcieri@...>

It seems there are 3 ways of defining class methods (at least in common

12 messages 2011/02/16

[#378685] LiveAST: a pure Ruby 1.9.2 library obtaining live abstract syntax trees — "James M. Lawrence" <quixoticsycophant@...>

= LiveAST

13 messages 2011/02/18

[#378753] posix_mq : Problem installing on HPUX — Tadeusz Bochan <tad.bochan@...>

Hello,

13 messages 2011/02/20

[#378890] a, b = Array.new(2).map!{|x| data.dup} — Stefan Salewski <mail@...>

I think I can replace this code

19 messages 2011/02/23
[#378892] Re: a, b = Array.new(2).map!{|x| data.dup} — niklas | brueckenschlaeger <niklas@...> 2011/02/23

Are you sure you can't rework your code to *not* copy data 5x? I assume

[#378899] Re: a, b = Array.new(2).map!{|x| data.dup} — Stefan Salewski <mail@...> 2011/02/23

On Thu, 2011-02-24 at 07:00 +0900, niklas | brueckenschlaeger wrote:

[#378941] Automatic question generator libs in Ruby Language — Sniper Abandon <sathish.salem.1984@...>

is there any Automatic question generator libraries in Ruby Language ?

20 messages 2011/02/24
[#379058] Re: Automatic question generator libs in Ruby Language — Sniper Abandon <sathish.salem.1984@...> 2011/02/27

suppose if i have a paragraph (arround 250 words)

[#379172] Re: Automatic question generator libs in Ruby Language — Shadowfirebird <shadowfirebird@...> 2011/03/01

> i want to get all the possible question from that paragraph

[#379174] Re: Automatic question generator libs in Ruby Language — Peter Zotov <whitequark@...> 2011/03/01

On Tue, 1 Mar 2011 19:31:36 +0900, Shadowfirebird wrote:

[#379175] Re: Automatic question generator libs in Ruby Language — Adam Prescott <mentionuse@...> 2011/03/01

On Tue, Mar 1, 2011 at 10:55 AM, Peter Zotov <whitequark@whitequark.org>wrote:

[#379177] Re: Automatic question generator libs in Ruby Language — Peter Zotov <whitequark@...> 2011/03/01

On Tue, 1 Mar 2011 20:02:13 +0900, Adam Prescott wrote:

[#379179] Re: Automatic question generator libs in Ruby Language — Adam Prescott <mentionuse@...> 2011/03/01

On Tue, Mar 1, 2011 at 12:28 PM, Peter Zotov <whitequark@whitequark.org>wrote:

[#378949] why is $1 in a grep() equal to nil? — 7stud -- <bbxx789_05ss@...>

class DataSource

16 messages 2011/02/24
[#378953] Re: why is $1 in a grep() equal to nil? — Eric Christopherson <echristopherson@...> 2011/02/25

On Thu, Feb 24, 2011 at 2:59 PM, 7stud -- <bbxx789_05ss@yahoo.com> wrote:

[#378958] parsing rule for this code? — 7stud -- <bbxx789_05ss@...>

1)

11 messages 2011/02/25

[#379000] Symbol#to_proc helping out with #select to beat Scala-s solution — Jarmo Pertman <jarmo.p@...>

Hey!

9 messages 2011/02/25

[#379074] finding a tag in a binary file — rob stanton <tnotnats@...>

I have a binary file in which I'd like to find multiple strings of 10

12 messages 2011/02/27

Re: Editing a text file and then replacing existing file

From: Robert Klemme <shortcutter@...>
Date: 2011-02-02 16:52:41 UTC
List: ruby-talk #377957
On Wed, Feb 2, 2011 at 5:31 PM, Jeremy Bopp <jeremy@bopp.net> wrote:
> On 2/2/2011 9:53 AM, Bob Hatch wrote:
>> I have a script that I use to find text in a file and then delete the
>> line(s) the text is on. That part works. What I have been unsuccessfully
>> trying to do is take the new text and overwrite the existing file that I
>> did the search and delete on. Here is my existing code that finds the
>> lines and deletes them:
>>
>> path=3D'c:\ruby192\my_projects\IIS_Logs\ex11012607.log'
>> search_text =3D %r{/memberinfo/downline/tree/can.asp}
>> search_text2 =3D %r{/memberinfo/downline/tree/disto.asp}
>> search_text3 =3D %r{/styles/default.css}
>> search_text4 =3D %r{/scripts/generalscript.js}
>> search_text5 =3D %r{/memberinfo/downline/tree/upl.asp}
>> search_text6 =3D %r{/scripts/wz_jsgraphics.js}
>> search_text7 =3D %r{/memberinfo/downline/tree/disfo2.asp}
>> search_text8 =3D %r{/styles/png.htc}
>> File.open(path, 'a+') do |file|
>> =A0 file.each_line do |line|
>> =A0 =A0 =A0 puts(line) unless line =3D~ search_text ||
>> =A0 puts(line) unless line =3D~ search_text2 ||
>> =A0 =A0 =A0 puts(line) unless line =3D~ search_text3 ||
>> =A0 =A0 =A0 puts(line) unless line =3D~ search_text4 ||
>> =A0 =A0 =A0 puts(line) unless line =3D~ search_text5 ||
>> =A0 =A0 =A0 puts(line) unless line =3D~ search_text6 ||
>> =A0 =A0 =A0 puts(line) unless line =3D~ search_text7 ||
>> =A0 =A0 =A0 puts(line) unless line =3D~ search_text8
>> =A0 =A0end
>> file.write(line)
>> file.close
>> end
>
> First of all, let's simplify this code by using a single regexp that
> will match all lines of interest. =A0This uses the x option for the regex=
p
> construction so that the format is a little more legible:
>
> search_text =3D %r{
> =A0/memberinfo/downline/tree/can.asp =A0 =A0|
> =A0/memberinfo/downline/tree/disto.asp =A0|
> =A0/styles/default.css =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0|
> =A0/scripts/generalscript.js =A0 =A0 =A0 =A0 =A0 =A0|
> =A0/memberinfo/downline/tree/upl.asp =A0 =A0|
> =A0/scripts/wz_jsgraphics.js =A0 =A0 =A0 =A0 =A0 =A0|
> =A0/memberinfo/downline/tree/disfo2.asp |
> =A0/styles/png.htc
> }x
>
> Beware that the x option will cause any unescaped whitespace in your
> regexp to be ignored, so you need to take care if you need to match a
> file path that has spaces in it.
>
> With this regexp you would be able to use a single puts(line) call.
> BTW, you should really try to avoid duplicated code like you have with
> your puts lines in general. =A0Alarm bells should go off in your head if
> you see that sort of thing. =A0More importantly though, you need to take =
a
> closer look at what you're doing there with the || operator. =A0That's no=
t
> exactly doing what you seem to intend. ;-)
>
> Now we get to the meat of your problem. =A0You can't write to the file
> from which you're reading without manually maintaining at least 2
> pointers into the file, one for your current read position and another
> for your write position. =A0It's *much* easier actually create a new file
> to hold the filtered data and then use that file to replace the original:
>
> File.open(source_path) do |infile|
> =A0File.open(temp_path, "w") do |outfile|
> =A0 =A0infile.each_line do |line|
> =A0 =A0 =A0outfile.write(line) unless line =3D~ search_text
> =A0 =A0end
> =A0end
> end
> File.rename(temp_path, source_path)

Just an additional perspective: for things like this it's probably
easier to use GNU fgrep:

$ fgrep -v -f search_texts input > output
$ mv input input~ && mv output input

Kind regards

robert

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

In This Thread

Prev Next