From: Hongyi Zhao on
On Tue, 24 Nov 2009 12:22:17 -0800 (PST), Rakesh Sharma
<sharma__r(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 :.
From: Hongyi Zhao on
On Tue, 24 Nov 2009 17:34:47 +0800, Hongyi Zhao
<hongyi.zhao(a)gmail.com> wrote:

>Suppose the following should be the final result I want:
>
>...
>here_comes_line_a
>...
>here_comes_line_b
>...

Oops, in fact, I want to obtain the following final result:

....
....
here_comes_line_a
here_comes_line_b
....

I.e., line_a should appear just immediately before line_b. Thanks
again.
--
..: Hongyi Zhao [ hongyi.zhao AT gmail.com ] Free as in Freedom :.
From: Ed Morton on
Hongyi Zhao wrote:
> On Tue, 24 Nov 2009 17:34:47 +0800, Hongyi Zhao
> <hongyi.zhao(a)gmail.com> wrote:
>
>> Suppose the following should be the final result I want:
>>
>> ...
>> here_comes_line_a
>> ...
>> here_comes_line_b
>> ...
>
> Oops, in fact, I want to obtain the following final result:
>
> ...
> ...
> here_comes_line_a
> here_comes_line_b
> ...
>
> I.e., line_a should appear just immediately before line_b. Thanks
> again.

So, is there actually any swapping involved or do you just want to save line_a
and print it before line_b?

Ed.
From: Hongyi Zhao on
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

Best regards.
--
..: Hongyi Zhao [ hongyi.zhao AT gmail.com ] Free as in Freedom :.
From: Ed Morton on
Hongyi Zhao 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
>
> Best regards.

OK, so you don't actually want to swap anything, you want to find "line_b" and
move it to earlier in the file, specifically right after "line_a", right? try this:

tac file | awk '/line_b/{b=$0 RS; next} /line_a/{printf "%s",b} 1' | tac

Ed.