Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

CRLF in Windows

Status
Not open for further replies.

stevexff

Programmer
Mar 4, 2004
2,110
GB
I'm using ActiveState perl 5.1.8 on a Win2003 server box.

I had to parse a tab-delimited file created by Excel. Some of the cells in the spreadsheet contained LF characters. Each record was separated by CRLF. When I read the file with perl, it split the lines on LF, not CRLF as expected. $/ was LF, and if I changed it to anything else (CR, CRLF) it only found one huge record.

Even reading the whole file in and looking for CR failed to find any occurrences of CR. They seem to have been stripped out.

I know the CRs are in the file, as I can see them with a hex editor and notepad displays the records correctly. But if you open the file in Visual Studio, it has the same problem as perl and splits the lines on LF.

Any ideas, anyone?
 
TTry splitting on \n\r, or \r\n its one of them
--Paul

Nancy Griffith - songstress extraordinaire,
and composer of the snipers anthem "From a distance ...
 
Paul

Thanks for the tip, but I'd already tried all those combinations. Even if you slurp in the whole file, there are no CRs in it at all. It's almost like they are being filtered out in the I/O before perl gets to see them...

Meanwhile, I have saved the Excel files as XML and parsed them that way instead (XML::Simple) which seems to cope with them OK.

Cheers
 
You can try writing a new file first without the \r in the new file, then process.

Code:
$| = 1;

open(NEWFILE, "> New_File_Name") || die "ERROR: Unable to write new file [New_File_Name]:$?\n";
select(NEWFILE); $| = 1;

while (<>) {
     chomp;
     s/\r//g;
     print NEWFILE "$_\n";
}
select(NEWFILE); $| = 1;
select(STDOUT); $| = 1;
close(NEWFILE);
exit(0);
[code]

Michael Libeson
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top