From: Hongyi Zhao on
On Thu, 26 Nov 2009 13:37:15 -0800 (PST), w_a_x_man
<w_a_x_man(a)yahoo.com> wrote:

>Yes.
>ruby -i.bak -pe'gsub( "c", "x" )' data
>
>This makes a backup file named "data.bak" and replaces
>each "c" with "x".

Good, thanks again. I want to do the inplace edit without backup file
generated. What option should I use?
--
..: Hongyi Zhao [ hongyi.zhao AT gmail.com ] Free as in Freedom :.
From: Rakesh Sharma on
On Nov 26, 7:33 am, Hongyi Zhao <hongyi.z...(a)gmail.com> wrote:
> On Tue, 24 Nov 2009 12:22:17 -0800 (PST), Rakesh Sharma
>
> <sharma...(a)hotmail.com> wrote:
> >Provided there's atleast one line between line_a & line_b we can do
> >this:
>
> In my case, the line_b sometimes just immediately appear before
> line_a, i.e.,
>
> line_b
> line_a
>
> How should your code be changed in order to deal with both cases?
>
>
>
> >sed -e '
> >   /\n/b
> >   /re1/,/re2/!b
> >   /re2/!H;/re1/h;/re2/!d
> >   p;g;s/\n.*//;H;g;D
> >' yourfile
>
> So complicated for me to understand, any hints on your code?
>
> Best regards.
> --
> .: Hongyi Zhao [ hongyi.zhao AT gmail.com ] Free as in Freedom :.


As you have stated that you want to align line_a then line_b no matter
where they come in the file.
& they occur just once in the file.

sed -ne '
/line_a/ba
/line_b/bb
p;d

:a
p;n;/line_b/bspin
:notb
/line_b/!{
H;n
bnotb
}
p;g;s/.//;bspin

:b
h;n
:nota
/line_a/!{
p;n
bnota
}
G;bspin

:spin
p;$d;n
bspin
' yourfile

-- Rakesh
From: WANG Cong on
On 11/26/09 11:51, Hongyi Zhao <hongyi.zhao(a)gmail.com> wrote:

> On Wed, 25 Nov 2009 21:02:04 -0600, Ed Morton <mortonspam(a)gmail.com>
> wrote:
>
>>So, is there actually any swapping involved or do you just want to save line_a
>>and print it before line_b?
>
> 1- If line_b appears just immediately after line_a, then do nothing.
>
> 2- In other cases, move line_b to make sure it just immediately after
> line_a
>

awk 'BEGIN{a=b=i=0}/line_a/{a=i;} /line_b/{b=i;} {l[i++]=$0;} END{t=l[b];for(i=0;i<NR;i++){if(i!=b)print l[i];if(i==a)print t;}}' your_file


--
Live like a child, think like the god.

From: WANG Cong on
On 11/27/09 10:16, Hongyi Zhao <hongyi.zhao(a)gmail.com> wrote:

> On Thu, 26 Nov 2009 13:37:15 -0800 (PST), w_a_x_man
> <w_a_x_man(a)yahoo.com> wrote:
>
>>Yes.
>>ruby -i.bak -pe'gsub( "c", "x" )' data
>>
>>This makes a backup file named "data.bak" and replaces
>>each "c" with "x".
>
> Good, thanks again. I want to do the inplace edit without backup file
> generated. What option should I use?

'-i' without anything, the same as 'sed'.

--
Live like a child, think like the god.

From: Hongyi Zhao on
On Fri, 27 Nov 2009 23:42:27 +0800, WANG Cong
<xiyou.wangcong(a)gmail.com> wrote:

>'-i' without anything, the same as 'sed'.

Does the gets(nil) statement work with '-i' switch? In my case, I
always meet the error like this:

test.rb:9:in `gets': Can't do inplace edit without backup (fatal)
from test.rb:9

Why?
--
..: Hongyi Zhao [ hongyi.zhao AT gmail.com ] Free as in Freedom :.