[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Deleting a line in a file

Justin To

6/25/2008 5:49:00 PM

Hi, how do I delete a line in a file and remove the empty space
afterward?

Thanks!
--
Posted via http://www.ruby-....

8 Answers

Marc Heiler

6/25/2008 6:01:00 PM

0

There are many ways to solve this.
One I use often (which is not very elegant but rather easy) is to use
File.readlines on your file, find the line in that array to kill it, and
save the array to a file finally again.

BTW what do you mean with "empty space"? I guess it will be easy if you
make a small file as example :)
--
Posted via http://www.ruby-....

Justin To

6/25/2008 6:02:00 PM

0

> BTW what do you mean with "empty space"? I guess it will be easy if you
> make a small file as example :)

File:
a
b
c
d

program: delete(b)

File:
a
<-- empty space
c
d

should be...

File:
a
c
d

Thanks!
--
Posted via http://www.ruby-....

Marc Heiler

6/25/2008 6:18:00 PM

0

This should get you started. There are probably 1000 ways but well,
someone else can do that :)

The test_file had this content:
a
b
c
d

The new_file has
a
b
d


class DeleteLine
def initialize(read_this_file = 'test_file')
@file = File.readlines(read_this_file)
delete_line 3
save_to
end
def delete_line(which_line = 1)
@file.delete_at(which_line-1) # we may count first line as 1, second
line as 2 etc.. otherwise remove the -1 part
end
def save_to(this_file = 'new_file')
File.open(this_file, 'w+') {|f| f.write @file.join() }
end
end

DeleteLine.new
--
Posted via http://www.ruby-....

Reid Thompson

6/25/2008 7:13:00 PM

0

On Thu, 2008-06-26 at 02:49 +0900, Justin To wrote:
> Hi, how do I delete a line in a file and remove the empty space
> afterward?
>
> Thanks!

grep -v "character string unique to line to be removed" file > tmpfile
mv tmpfile file

Jimmy Kofler

6/26/2008 3:35:00 PM

0

> Justin To wrote:
> Hi, how do I delete a line in a file and remove the empty space
> afterward?
>
> Thanks!

Some additional in-place file editing options on Unix:

/bin/ed -s file.txt <<< $'/b/d\nw' # delete first line containing "b"

/bin/ed -s file.txt <<< $'g/b/d\nw' # delete all lines containing "b"
/usr/bin/sed -i "" -e '/b/d' file.txt

/bin/ed -s file.txt <<< $'2,2d\nw' # delete second line
/usr/bin/sed -i "" -e '2,2d' file.txt

More on the ed text editor here:
http://bash-hackers.org/wiki/doku.php?id=how...


Using Ruby from the command line:

ruby -p -i -e 'm ||= 0; m == 1 ? $_ : $_.sub!(/^.*2.*$/m, "") || $_; m =
1 unless $~.nil?' file.txt
ruby -p -i -e '$_.sub!(/^.*b.*$/m, "")' file.txt
ruby -p -i -e '$_.sub!(/^.*$/m, "") if $. == 2' file.txt

For more information on selective deletion of certain lines see:
http://www.fepus.net/rub...


Cheers,

j.k.

--
Posted via http://www.ruby-....

David Hartung

10/10/2011 2:37:00 AM

0

On 10/09/2011 11:25 AM, Gary DW wrote:
> In article<j6r8ka$k7b$1@dont-email.me>, ob@in.valid
> says...
>>>> And jobs?
>>>>
>>>> You do realize government's record there has been shameful, don;t you?
>>>>
>>>> Of the first stimulus less than 4% went to "shovel ready jobs".
>>>
>>>
>>> Most went to your moronic tax cuts, ja know.
>>
>> No, I do not know that, and neither do you.
>>
>> What you have done is LIE, and in an easily handled fashion:
>>
>> http://online.wsj.com/public/resources/documents/STIMULUS_FINAL...
>>
>> Now don't you feel like a silly little ass?
>>
>>
>
>
>
> According to your own cite, behind state aid to
> Medicare... the second largest expense was the tax cuts.
>
> Perhaps "most" was hyperbole, but it WAS still one of
> the biggest tax cuts in American history, if not the
> biggest.
>
> http://www.usnews.com/opinion/blo...
> schlesinger/2009/02/12/is-obama-stimulus-plan-also-the-
> biggest-tax-cut-ever

You support your statement of opinion with another opinion piece?

> or http://tinyurl.c...

You are aware that your second cite is identical to the firsr?

> Why anyone gives you snibbling cry-babies any of your
> agenda, is a mystery to me. You cry until you get your
> way, cry some more as it is passed, and then cry even
> more at what a miserable failure it was. YOU GOT THE
> BIGGEST TAX CUT IN AMERICAN HISTORY AND ALL YOU DO IS
> BAWL ABOUT HOW MUCH IT COST AND HOW IT DIDNT HELP.

The biggest tax cut in history? Are you absolutely certain:

It seems that the Washington Post disagrees with you:

http://tinyurl.c...

Gary DW

10/10/2011 2:43:00 AM

0

In article <4e91f1be$0$1674$742ec2ed@news.sonic.net>, rfischer@sonic.net
says...
>
> Old Baye <ob@in.valid> wrote:
> >On 10/9/2011 9:25 AM, Gary DW wrote:
> >> In article<j6r8ka$k7b$1@dont-email.me>, ob@in.valid
> >> says...
> >>>>> And jobs?
> >>>>>
> >>>>> You do realize government's record there has been shameful, don;t you?
> >>>>>
> >>>>> Of the first stimulus less than 4% went to "shovel ready jobs".
> >>>>
> >>>>
> >>>> Most went to your moronic tax cuts, ja know.
> >>>
> >>> No, I do not know that, and neither do you.
> >>>
> >>> What you have done is LIE, and in an easily handled fashion:
> >>>
> >>> http://online.wsj.com/public/resources/documents/STIMULUS_FINAL...
> >>>
> >>> Now don't you feel like a silly little ass?
> >>>
> >> According to your own cite, behind state aid to
> >> Medicare... the second largest expense was the tax cuts.
> >
> >According to reality, the bulk of the "stimulus" went to entitlements
>
> Rightard rule #1: lie, lie again, lie more, lie about lying.
>
> You know it's all about hate when the rightard whines about tax cuts.


He's coming unglued.

His own frigging cite said they were tax cuts and he's having a
conniption because they aren't good enough tax cuts.

Old Baye needs a time out and a dunce hat in the corner... perhaps a
pretty squeezy toy will calm him?



Gary DW

10/10/2011 3:06:00 AM

0

In article
<uq2dnRuFaNHFxw_TnZ2dnUVZ_qednZ2d@giganews.com>,
david@hotmai*l.com says...
>
> On 10/09/2011 11:25 AM, Gary DW wrote:
> > In article<j6r8ka$k7b$1@dont-email.me>, ob@in.valid
> > says...
> >>>> And jobs?
> >>>>
> >>>> You do realize government's record there has been shameful, don;t you?
> >>>>
> >>>> Of the first stimulus less than 4% went to "shovel ready jobs".
> >>>
> >>>
> >>> Most went to your moronic tax cuts, ja know.
> >>
> >> No, I do not know that, and neither do you.
> >>
> >> What you have done is LIE, and in an easily handled fashion:
> >>
> >> http://online.wsj.com/public/resources/documents/STIMULUS_FINAL...
> >>
> >> Now don't you feel like a silly little ass?
> >>
> >>
> >
> >
> >
> > According to your own cite, behind state aid to
> > Medicare... the second largest expense was the tax cuts.
> >
> > Perhaps "most" was hyperbole, but it WAS still one of
> > the biggest tax cuts in American history, if not the
> > biggest.
> >
> > http://www.usnews.com/opinion/blo...
> > schlesinger/2009/02/12/is-obama-stimulus-plan-also-the-
> > biggest-tax-cut-ever
>
> You support your statement of opinion with another opinion piece?
>
> > or http://tinyurl.c...
>
> You are aware that your second cite is identical to the firsr?



I included that for YOUR benefit, you babbling idiot.

Notice it is smaller?


>
> > Why anyone gives you snibbling cry-babies any of your
> > agenda, is a mystery to me. You cry until you get your
> > way, cry some more as it is passed, and then cry even
> > more at what a miserable failure it was. YOU GOT THE
> > BIGGEST TAX CUT IN AMERICAN HISTORY AND ALL YOU DO IS
> > BAWL ABOUT HOW MUCH IT COST AND HOW IT DIDNT HELP.
>
> The biggest tax cut in history? Are you absolutely certain:
>
> It seems that the Washington Post disagrees with you:
>
> http://tinyurl.c...

David whines about an opinion piece and then counters
with an OPINION piece.

JFC.

Try to think every once in awhile.




--
Modern conservatism is a cult. Neither it nor its voters
can be reasoned with. Attempting to pragmatically
compromise with them in the hopes of
deflecting criticism, "changing Washington" and winning
"independent" voters is a waste of time.

-Digby