CR vs LF perl 解析 [英] CR vs LF perl parsing

查看:63
本文介绍了CR vs LF perl 解析的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 perl 脚本,它解析一个文本文件并将它每行分解成一个数组.当每一行都由 LF 终止时它工作正常,但是当它们由 CR 终止时,我的脚本处理不正确.如何修改此行以解决此问题

I have a perl script which parses a text file and breaks it up per line into an array. It works fine when each line are terminated by LF but when they terminate by CR my script is not handling properly. How can I modify this line to fix this

my @allLines = split(/^/, $entireFile);

我的文件混合了以下任一行结束 LF 或结束 CR 它只是在以 CR 结尾时折叠所有行

edit: My file has a mixture of lines with either ending LF or ending CR it just collapses all lines when its ending in CR

推荐答案

Perl 可以使用内置的 :crlf PerlIO 层:

Perl can handle both CRLF and LF line-endings with the built-in :crlf PerlIO layer:

open(my $in, '<:crlf', $filename);

将自动将 CRLF 行尾转换为 LF,并保持 LF 行尾不变.但是只有 CR 的文件是奇怪的人.如果您知道该文件仅使用 CR,那么您可以将 $/ 设置为"\r" 它将逐行读取(但不会将 CR 更改为 LF).

will automatically convert CRLF line endings to LF, and leave LF line endings unchanged. But CR-only files are the odd-man out. If you know that the file uses CR-only, then you can set $/ to "\r" and it will read line-by-line (but it won't change the CR to a LF).

如果您必须处理未知行尾的文件(甚至单个文件中的混合行尾),您可能需要安装 PerlIO::eol 模块.然后你可以说:

If you have to deal with files of unknown line endings (or even mixed line endings in a single file), you might want to install the PerlIO::eol module. Then you can say:

open(my $in, '<:raw:eol(LF)', $filename);

它会在您阅读文件时自动将 CR、CRLF 或 LF 行尾转换为 LF.

and it will automatically convert CR, CRLF, or LF line endings into LF as you read the file.

另一种选择是将 $/ 设置为 undef,这将一次性读取整个文件.然后将其拆分为 /\r\n?|\n/.但前提是文件小到可以放入内存.

Another option is to set $/ to undef, which will read the entire file in one slurp. Then split it on /\r\n?|\n/. But that assumes that the file is small enough to fit in memory.

这篇关于CR vs LF perl 解析的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆