<FILE> and CR-LF 
Author Message
 <FILE> and CR-LF

Hi,

I have a bit of a problem that I would like to solve using perl.
However, I am unable to figure out exactly how to do one tiny thin.
Here's the scenario:

I have an input file containing some textual tokens I would like to
read and deal with.  Normally, I use the loop:

while (<STDIN>) {

 [ ... Parse stuff ... ]

 print STDOUT "Result\n";

Quote:
}

and this works well.  However, the input fie containing the textual
tokens may contain some binary data, and may be generated on a
Macintosh or PC running DOS.  (It's a postscript file.)  When
transferring the file to a Unix machine, I must FTP it in binary mode,
or else the encoded data is messed up.

So, now I have an input file containing these textual tokens separted
by either CR, LF, or CR-LF; and I need to loop through it line by
line.  I am unable to simply use something like 'tr' to transpose the
LF into CR, as this may mess up the binary data.

If I run perl with the -l flag, I can make sure that my output
contains the same end-of-line char on each line that the input file
contains.  The big problem I have is getting the command '<STDIN>' to
break the input file on EITHER CR or LF.

It is possible to set the input end-of-line char ($\ or $/, can't
remember which) to be a specific char.  However, I would like to be
able to set it to split at either 1 of 2 chars.  

Does anybody know if this is possible?

Thanks!

Ryan Troll
Computing Services
Carnegie Mellon University



Sun, 09 Nov 1997 03:00:00 GMT  
 <FILE> and CR-LF

[snip]
: So, now I have an input file containing these textual tokens separted
: by either CR, LF, or CR-LF; and I need to loop through it line by
: line.  I am unable to simply use something like 'tr' to transpose the
: LF into CR, as this may mess up the binary data.
[snip]
: It is possible to set the input end-of-line char ($\ or $/, can't
: remember which) to be a specific char.  However, I would like to be
: able to set it to split at either 1 of 2 chars.  

: Does anybody know if this is possible?

  Unless your input file is very large, the easiest approach may be to
undefine the input record separator, read the whole thing into one
string and then parse it into lines with the perl regular expressions,
something like this:

# WARNING! UNTESTED CODE
$/ = undef;
$_ = <>;
while (s/^([^\n\r]*)(\r|\r\n|\n)//o) {
  $line = $1 || "";
  # process line ...

Quote:
}

  If you don't care about recognizing empty lines, you can use [\n\r]+
for your end of line pattern.

Bruce



Wed, 12 Nov 1997 03:00:00 GMT  
 
 [ 2 post ] 

 Relevant Pages 

1. CR LF to <br> in REGEX

2. Reading a file without <CR><LF>

3. Sed or Perl to filter out all <CR><LF> in big file?

4. Converting LF to CR+LF

5. DOS Perl convering LF to CR/LF

6. CRLF->LF conversion happens even on Solaris

7. unwanted CR/LF translation in FILE UPLOAD

8. CR/LF Text-File

9. <<<<<< HELP >>>>>>>>

10. Input via STDIN without using <CR>

11. Read input from STDIN without <CR>

12. HELP: force a <CR> in DOS

 

 
Powered by phpBB® Forum Software