how to remove cr/lf ??? 
Author Message
 how to remove cr/lf ???

hello perl-gurus
simpel question: a perl-script writes the content of form-fields in a file
and the entries look like this:

user-input-field1
user-input-field2
user-input-field3
........
so all the entries have cr/lf. i'd like them to have in the file separated
by ";" ex:
user-input-field1;user-input-field2;user-input-field3;user........

my print-command for the file looks like this:

print FILETO $outbuffer;

how can i eliminate the cr/lf and replace it by a separator ??

thanx alot for your help

hubert



Fri, 18 Jan 2002 03:00:00 GMT  
 how to remove cr/lf ???
Hi Hubert,

Quote:

> user-input-field1
> user-input-field2
> user-input-field3
> ........
> so all the entries have cr/lf. i'd like them to have in the file separated
> by ";" ex:
> user-input-field1;user-input-field2;user-input-field3;user........

> my print-command for the file looks like this:

> print FILETO $outbuffer;

> how can i eliminate the cr/lf and replace it by a separator ??

maybe

    $outbuffer =~ s /\r?\n/;/;

Rgeards
Alex

--
Ich studiere  Elektrotechnik (Technische  Informatik) an der RWTH Aachen
und bin ein guter Perl-Programmierer (arbeite seit 4 Jahren als Intranet-
Entwickler). Kann auch C, Java, SQL, JavaScript und HTML, CGI und TCP/IP.
Ich suche eine gut bezahlte  Diplomstelle in Aachen, Koeln oder Umgebung.



Fri, 18 Jan 2002 03:00:00 GMT  
 how to remove cr/lf ???

Quote:

> hello perl-gurus
> simpel question: a perl-script writes the content of form-fields in a file
> and the entries look like this:

> user-input-field1
> user-input-field2
> user-input-field3
> ........
> so all the entries have cr/lf. i'd like them to have in the file separated
> by ";" ex:
> user-input-field1;user-input-field2;user-input-field3;user........

> my print-command for the file looks like this:

> print FILETO $outbuffer;

$outbuffer =~ s/\n/;/g;
print FILETO $outbuffer;

(This assumes your machine treats \n as CR-LF. This is the case if you're
on a PC.)


** Remove the REMOVE in my address address to reply reply  **



Fri, 18 Jan 2002 03:00:00 GMT  
 how to remove cr/lf ???


Quote:

> > ........
> > so all the entries have cr/lf. i'd like them to have in the file separated
> > by ";" ex:
> > user-input-field1;user-input-field2;user-input-field3;user........

> > my print-command for the file looks like this:

> > print FILETO $outbuffer;

> > how can i eliminate the cr/lf and replace it by a separator ??

> maybe

>     $outbuffer =~ s /\r?\n/;/;

Maybe not.  This newsgroup is for facts, not speculation.

Quote:
> Rgeards
> Alex

> --
> Ich studiere  Elektrotechnik (Technische  Informatik) an der RWTH Aachen
> und bin ein guter Perl-Programmierer (arbeite seit 4 Jahren als Intranet-
> Entwickler).

In that case, maybe your forgetting to append the '/g' modifier is an
oversight.  Just as you overlooked testing your suggestion before
posting it.

What a waste of bandwidth!

--
(Just Another Larry) Rosler
Hewlett-Packard Laboratories
http://www.hpl.hp.com/personal/Larry_Rosler/



Fri, 18 Jan 2002 03:00:00 GMT  
 how to remove cr/lf ???
Dan Wilga writes ..

Quote:
>$outbuffer =~ s/\n/;/g;
>print FILETO $outbuffer;

>(This assumes your machine treats \n as CR-LF. This is the case if you're
>on a PC.)

a PC ? .. so you mean if I run Linux or NT on my PC then my line endings
are still the same as DOS ? .. oh man - what a ripoff - I'm taking this
thing back

why assume anything .. Ming knows which characters to replace .. why not
replace them by name - explicitly ?

using the vars from the original question we get

  $outbuffer =~ s/\cM\cJ/;/g;
  print FILETO $outbuffer;

--
 jason - remove all hyphens for email reply -



Sat, 19 Jan 2002 03:00:00 GMT  
 how to remove cr/lf ???

Quote:

> Dan Wilga writes ..
> >(This assumes your machine treats \n as CR-LF. This is the case if
> >you're on a PC.)

> a PC ? .. so you mean if I run Linux or NT on my PC then my line
> endings are still the same as DOS ?

If you run NT, then yes. Of course. NT, Win9* and DOS all use crlf for
line endings. Linux (and (?:Free|Net)BSD, BSD386, Solaris, AIX, etc.)
don't; they use lf only. (Yes, there was an AIX for IBM's PS/2
machines.)

Cheers,
Philip



Sat, 19 Jan 2002 03:00:00 GMT  
 how to remove cr/lf ???

Quote:

>>(This assumes your machine treats \n as CR-LF. This is the case if you're
>>on a PC.)

>a PC ? .. so you mean if I run Linux or NT on my PC then my line endings
>are still the same as DOS ? .. oh man - what a ripoff - I'm taking this
>thing back

You're easily disturbed, if a change in definition of line endings
bothers you that much.

        Bart.



Sat, 19 Jan 2002 03:00:00 GMT  
 how to remove cr/lf ???

Quote:
> $outbuffer =~ s/\n/;/g;
> print FILETO $outbuffer;

> (This assumes your machine treats \n as CR-LF. This is the case if you're
> on a PC.)

my machine seems to treat cr-lf as \r so i guess
s/\r\n/\n/g; would be ok too

interestingly this seems to happen most when i use a textarea field
to get input

jagadish



Sat, 19 Jan 2002 03:00:00 GMT  
 how to remove cr/lf ???

Quote:


> >>(This assumes your machine treats \n as CR-LF. This is the case if you're
> >>on a PC.)

> >a PC ? .. so you mean if I run Linux or NT on my PC then my line endings
> >are still the same as DOS ? .. oh man - what a ripoff - I'm taking this
> >thing back

> You're easily disturbed, if a change in definition of line endings
> bothers you that much.

Umm, Bart, you do know that elephant was just gigging the
original poster?  The poster had made a similarly silly
statement about his software, and...

David
--

Senior computing specialist
mathematical statistician



Sat, 19 Jan 2002 03:00:00 GMT  
 how to remove cr/lf ???


...

Quote:
> if you have any documentation supporting your CRLF claim for NT then I'd
> like to see it .. in the absence of any I did some tests - have you
> tested this yourself ? .. it appears to use LF only

> in fact .. it does use LF only .. it understands CR+LF .. but if you open
> something with Notepad on NT and type some lines and hit your Return key
> at the ends of them .. then check the files .. they've only got LFs

That is contrary to my experience and expectation.  How did you check
the files?  If you used a Perl program, unless you binmode()ed the
files, the I/O system will convert CR+LF to LF.

You might also compare the size of the file with the length of a string
into which the whole file is read (using 'local $/;').  If they are the
same, you are right.  But they won't be.

--
(Just Another Larry) Rosler
Hewlett-Packard Laboratories
http://www.hpl.hp.com/personal/Larry_Rosler/



Sat, 19 Jan 2002 03:00:00 GMT  
 how to remove cr/lf ???
[Posted and a courtesy copy mailed.]



Quote:
> Larry Rosler writes ..
> >That is contrary to my experience and expectation.  How did you check
> >the files?  If you used a Perl program, unless you binmode()ed the
> >files, the I/O system will convert CR+LF to LF.

> excellent .. my own stupid mistake .. thanks for that (it was also
> contrary to my expectation - but I thought I'd tested it *8^)

> however .. I'm still confused by my second test .. because isn't perl
> meant to map "\n" to whatever line ending character sequence it is that
> the operating system uses ?

<SNIP of confusion>

Quote:
> don't worry .. I've just experimented myself out of my black hole .. the
> transformation is only done on the read/write to a textual filehandle ..
> and the transformation is not done on "\n" but rather on the LF character
> itself ..

The internal representation of "\n" *is* the LF character itself (on
Unix and Windows/DOS; in MacPerl it's the CR character; I won't speak
for any other OSes).  But you seldom need to know more than that it is a
single /\s/ character.

Quote:
>            so that even

>   print TEXTFILEHANDLE "one\015\012";

> will output

> one\015\015\012

> to the file in question .. in which case - I'd have to say that the
> perlop section is lacking - probably ActiveState's fault more than
> anyone's - there's no mention of line ending characters in the win32 FAQs

There is a very weird documentation bug in the ActiveState distribution,
at least through Build 517.  I haven't downloaded Build 518 yet to see
if it is fixed.  I sent them a bug report recently, but haven't heard a
response.

The bug is that their version of perltoc.html has no entry for perlport
(of all things to omit!).  Edit .../perl/html/perltoc.html and duplicate
and edit the entry for perlpod to include perlport.  Then reload it and
look at the first entry in perlport:  ISSUES  Newlines.  Voil -- it's
all there!

--
(Just Another Larry) Rosler
Hewlett-Packard Laboratories
http://www.hpl.hp.com/personal/Larry_Rosler/



Sat, 19 Jan 2002 03:00:00 GMT  
 how to remove cr/lf ???
Philip 'Yes, that's my address' Newton writes ..

Quote:

>> Dan Wilga writes ..
>> >(This assumes your machine treats \n as CR-LF. This is the case if
>> >you're on a PC.)

>> a PC ? .. so you mean if I run Linux or NT on my PC then my line
>> endings are still the same as DOS ?

>If you run NT, then yes. Of course. NT, Win9* and DOS all use crlf for
>line endings.

if you have any documentation supporting your CRLF claim for NT then I'd
like to see it .. in the absence of any I did some tests - have you
tested this yourself ? .. it appears to use LF only

in fact .. it does use LF only .. it understands CR+LF .. but if you open
something with Notepad on NT and type some lines and hit your Return key
at the ends of them .. then check the files .. they've only got LFs

--



Sun, 20 Jan 2002 03:00:00 GMT  
 how to remove cr/lf ???
Bart Lateur writes ..

Quote:

>>>(This assumes your machine treats \n as CR-LF. This is the case if you're
>>>on a PC.)

>>a PC ? .. so you mean if I run Linux or NT on my PC then my line endings
>>are still the same as DOS ? .. oh man - what a ripoff - I'm taking this
>>thing back

>You're easily disturbed, if a change in definition of line endings
>bothers you that much.

I hope you're joking as much as I was

my disappointment was feigned in order to bring humour into the comment -
with the noble hope of making Dan Wilga think about what they write a bit
more before writing it .. inaccuracies are useless

--



Sun, 20 Jan 2002 03:00:00 GMT  
 how to remove cr/lf ???
S Jagadish writes ..

Quote:
>> $outbuffer =~ s/\n/;/g;
>> print FILETO $outbuffer;

>> (This assumes your machine treats \n as CR-LF. This is the case if you're
>> on a PC.)

>my machine seems to treat cr-lf as \r so i guess
>s/\r\n/\n/g; would be ok too

I doubt that any machine treats CRLF as "\r" .. machines talking DOS will
map "\n" to CRLF .. and other machines map "\n" to other things .. this
makes the use of "\n" to match one character a dangerous thing if the
script is EVER going to be used on another platform

you can see that your substitution

  s/\r\n/\n/g;

will change any CRLFs into LFs on a UNIX machine .. but on a DOS machine
will only match CRCRLFs

now .. this may actually be what you want .. because clearly then the
CRLFs will be left alone on DOS .. but as long as you are away of the
different circumstances .. I think on Macintosh it will match CRLFCRs ..
in which case it will leave the CRLFs which are NOT the Macintosh line
endings (to my knowledge)

Quote:
>interestingly this seems to happen most when i use a textarea field
>to get input

most network communication will end lines with CRLFs .. because the
textarea field is being transmitted across the network then the line
endings that a user enters in that field will be transmitted as CRLFs ..
the best way to get rid of these is something like this

  s/\cM\cJ/\n/g;

or numerically as

  s/\015\012/\n/g;

this will substitute the known CRLF character sequence in the stream for
the unknown (and dynamic) line ending sequence for the machine that the
code is running on

--



Sun, 20 Jan 2002 03:00:00 GMT  
 how to remove cr/lf ???
Larry Rosler writes ..

Quote:
>That is contrary to my experience and expectation.  How did you check
>the files?  If you used a Perl program, unless you binmode()ed the
>files, the I/O system will convert CR+LF to LF.

excellent .. my own stupid mistake .. thanks for that (it was also
contrary to my expectation - but I thought I'd tested it *8^)

however .. I'm still confused by my second test .. because isn't perl
meant to map "\n" to whatever line ending character sequence it is that
the operating system uses ?

if so then the following code confuses me

  my $x = "one\ntwo\nthree\n";

  $x =~ s/\cJ/\\cJ/g;
  $x =~ s/\cM/\\cM/g;

  print $x, "\n";

outputs

one\cJtwo\cJthree\cJ

or have I completely misunderstood perlop all these years ? .. just to
clarify - in my brain - on a DOS machine - when you code

  print "one\n";

what is output is

one\015\012

is that not correct ?

don't worry .. I've just experimented myself out of my black hole .. the
transformation is only done on the read/write to a textual filehandle ..
and the transformation is not done on "\n" but rather on the LF character
itself .. so that even

  print TEXTFILEHANDLE "one\015\012";

will output

one\015\015\012

to the file in question .. in which case - I'd have to say that the
perlop section is lacking - probably ActiveState's fault more than
anyone's - there's no mention of line ending characters in the win32 FAQs

time for an apology methinks

--



Sun, 20 Jan 2002 03:00:00 GMT  
 
 [ 19 post ]  Go to page: [1] [2]

 Relevant Pages 

1. Any Utility in Win32/Win95/Dos to remove CR or LF

2. Converting LF to CR+LF

3. DOS Perl convering LF to CR/LF

4. $/ = CR LF?

5. <FILE> and CR-LF

6. Binary data and CR-LF's in MSDOS

7. CR before login or any help that would lf before login

8. CR/LF

9. Question about formatting/(CR/LF) in PERL

10. Perl and CR+LF

11. CR LF to <br> in REGEX

12. CR/LF from a form

 

 
Powered by phpBB® Forum Software