knowledge-database (beta)

Current group: comp.editors

Removing newlines between patterns

Removing newlines between patterns  
mfyahya at gmail.com
 Re: Removing newlines between patterns  
Stephane CHAZELAS
 Re: Removing newlines between patterns  
James Hu
 Re: Removing newlines between patterns  
mfyahya at gmail.com
 Re: Removing newlines between patterns  
Kenny McCormack
From:mfyahya at gmail.com
Subject:Removing newlines between patterns
Date:23 Jan 2005 07:32:35 -0800
I have a large file with text like this
pattern line1
line2
line3
pattern line4
line6
pattern line7
pattern line8
line9
line10
line11
....

I want
pattern line1 line2 line3
pattern line4 line6
pattern line7 line8
pattern line9 line10 line 11

thanks for any help

Yahya
From:Stephane CHAZELAS
Subject:Re: Removing newlines between patterns
Date:Sun, 23 Jan 2005 15:46:35 +0000
2005-01-23, 07:32(-08), mfyahya@gmail.com:
> I have a large file with text like this
> pattern line1
> line2
> line3
> pattern line4
> line6
> pattern line7
> pattern line8
> line9
> line10
> line11
> ...
>
> I want
> pattern line1 line2 line3
> pattern line4 line6
> pattern line7 line8
> pattern line9 line10 line 11
[...]

With vim:

:%s/\n\(pattern\)\@!/ /

With awk:

awk '
NR > 1 {
if (/^pattern/) $0 = "\n" $0
else $0 = " " $0
}
{ printf "%s", $0 }
END {print ""}' < file.txt


--
Stéphane
From:James Hu
Subject:Re: Removing newlines between patterns
Date:Mon, 24 Jan 2005 02:18:05 -0600
On 2005-01-23, mfyahya@gmail.com wrote:
> I have a large file with text like this
> pattern line1
> line2
> line3
> pattern line4
> line6
> pattern line7
> pattern line8
> line9
> line10
> line11
> ...
>
> I want
> pattern line1 line2 line3
> pattern line4 line6
> pattern line7 line8
> pattern line9 line10 line 11
>
> thanks for any help
> Yahya

Stephane gave you a vim solution that worked for you. This is just
for those that may be looking for a plain vi solution.

First, make sure the global command will work for the last occurrence
of pattern by appending pattern to the end of the file:

Gopattern^[

Then, do the following global command, which joins all the lines that
begin with pattern up to the next line that begins with pattern:

:g/^pattern/,/^pattern/-1j

Then, remove the appended line.

Gdd


-- James

--
http://www.ungerhu.com/jxh/vi.html
From:mfyahya at gmail.com
Subject:Re: Removing newlines between patterns
Date:23 Jan 2005 08:32:07 -0800
Thanks it worked. I used Vim.
What does \@! do in :%s/\n\(pattern\)\@!/ / ?
Coudn't figure that out :)
From:Kenny McCormack
Subject:Re: Removing newlines between patterns
Date:Sun, 23 Jan 2005 16:45:06 GMT
In article <1106497927.085051.327360@f14g2000cwb.googlegroups.com>,
wrote:
>Thanks it worked. I used Vim.
>What does \@! do in :%s/\n\(pattern\)\@!/ / ?
>Coudn't figure that out :)
>

Seems to be like the "look ahead" features of Perl or Lex.
(Try "help @!")

Here's another VIM solution:

1G!Ggawk 'ORS=RT=="\n"?" ":RT' 'RS=\n(pattern)?'
   

Copyright © 2006 knowledge-database   -   All rights reserved