Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Help: Delete a single carriage return in a file, but not a double carriage return?

Reply
Thread Tools

Help: Delete a single carriage return in a file, but not a double carriage return?

 
 
Steve Anderson
Guest
Posts: n/a
 
      06-20-2004
I'd like to use perl to remove all single carriage returns, but leave all
double carriage returns intact in a text file.

For example, the following:

foo bar baz
baz bar foo baz
baz

baz foo bar
baz

should become:

foo bar baz baz bar foo baz baz

baz foo bar baz

It sounds simple, but I haven't been able to figure out how to do this.
Can anyone give me a hand?

Thanks,

Steve

 
Reply With Quote
 
 
 
 
Ben Morrow
Guest
Posts: n/a
 
      06-20-2004

Quoth Steve Anderson <>:
> I'd like to use perl to remove all single carriage returns, but leave all
> double carriage returns intact in a text file.
>
> For example, the following:
>
> foo bar baz
> baz bar foo baz
> baz
>
> baz foo bar
> baz
>
> should become:
>
> foo bar baz baz bar foo baz baz
>
> baz foo bar baz
>
> It sounds simple, but I haven't been able to figure out how to do this.
> Can anyone give me a hand?


#!/usr/bin/perl

$/ = $\ = "\n\n";

while (<>) {
chomp;
s/\n//g;
print;
}

__END__

Ben

--
All persons, living or dead, are entirely coincidental.
Kurt Vonnegut
 
Reply With Quote
 
 
 
 
Anno Siegel
Guest
Posts: n/a
 
      06-20-2004
Steve Anderson <> wrote in comp.lang.perl.misc:
> I'd like to use perl to remove all single carriage returns, but leave all

^^^^^^
> double carriage returns intact in a text file.
>
> For example, the following:
>
> foo bar baz
> baz bar foo baz
> baz
>
> baz foo bar
> baz
>
> should become:
>
> foo bar baz baz bar foo baz baz
>
> baz foo bar baz
>
> It sounds simple, but I haven't been able to figure out how to do this.
> Can anyone give me a hand?


You say "remove", but from your example you want to replace isolated
line feeds with blanks.

You can use lookbehind and lookahead to make sure a line feed is neither
preceded nor followed by another:

s/(?<!\n)\n(?!\n)/ /g;

Anno
 
Reply With Quote
 
Steve Anderson
Guest
Posts: n/a
 
      06-21-2004
On Sun, 20 Jun 2004 12:29:20 +0000, Anno Siegel wrote:

> You say "remove", but from your example you want to replace isolated
> line feeds with blanks.


You're absolutely right, my text was wrong, my example was right. Thanks
for seeing through it, and thanks for the solution.

Steve
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
"return delete (new int)" compile but "return delete (new X X C++ 4 07-19-2010 05:47 PM
Datagrid on load; replace all double single quote to single quote to display to user Eric Layman ASP .Net 3 04-14-2007 07:16 AM
Carriage Return added during return of large string from class method Xeno Campanoli Ruby 0 02-13-2006 08:39 PM
BoundColumn single DataBind equiv but not in a single row? Randall Parker ASP .Net 1 12-12-2005 04:11 PM
cannot convert parameter from 'double (double)' to 'double (__cdecl *)(double)' error Sydex C++ 12 02-17-2005 06:30 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57