为什么 Strawberry Perl 不删除这些换页字符?

发布于 2025-01-02 07:34:20 字数 483 浏览 2 评论 0原文

我目前正在 WinXP 上运行 Strawberry Perl,并且正在尝试处理 unix 格式的平面文件。平面文件使用换行符来分隔字段,并使用换页符来分隔记录。我正在尝试将 FF 转换为其他任何内容(CRLF、';'、TAB 等)。我尝试使用以下 perl 单行代码,但没有成功:

perl -p -e 's/\f/\r\n/g' < unix.txt > dos.txt
perl -p -e 's/\x0c/\x0d\x0a/g' < unix.txt > dos.txt
perl -p -e 's/\f/\t/g' < unix.txt > dos.txt

我注意到的唯一一件事是 dos.txt 最终将所有 LF 字符转换为 CRLF,但 FF 字符仍然存在。我什至尝试重新处理 dos.txt 文件,再次尝试替换 FF,但仍然没有骰子。我仍然是一个 Perl 新手,所以也许我错过了一些东西?有谁知道为什么上述命令不执行我想要的操作?

I'm currently running Strawberry Perl on WinXP, and I'm trying to process a unix-formatted flat file. The flat file uses line feed characters to delimit fields, and form feed characters to delimit a record. I am trying to convert the FF to anything else (CRLF, ';', TAB, etc). I have tried using the following perl one-liners with no success:

perl -p -e 's/\f/\r\n/g' < unix.txt > dos.txt
perl -p -e 's/\x0c/\x0d\x0a/g' < unix.txt > dos.txt
perl -p -e 's/\f/\t/g' < unix.txt > dos.txt

The only thing I've noticed is that the the dos.txt ends up with all the LF chars converted to CRLF, but the FF chars remain. I've even tried to reprocess the dos.txt file, again trying to replace the FF, but still no dice. I am still very much a perl newbie, so maybe I'm missing something? Does anyone know why the above commands don't do what I want them to do?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

假装不在乎 2025-01-09 07:34:20

问题是 Windows shell 不像 Unix shell 那样解释单引号。您应该在命令中使用双引号。

C:\ perl -e "print qq/foo\fbar/" > test.txt
C:\ type test.txt
foo♀bar
C:\ perl -pe 's/\f/__FF__/' < test.txt
foo♀bar
C:\ perl -pe "s/\f/__FF__/" < test.txt
foo__FF__bar

The problem is that the Windows shell doesn't interpret single quotes the way the Unix shell does. You should use double quotes in your commands.

C:\ perl -e "print qq/foo\fbar/" > test.txt
C:\ type test.txt
foo♀bar
C:\ perl -pe 's/\f/__FF__/' < test.txt
foo♀bar
C:\ perl -pe "s/\f/__FF__/" < test.txt
foo__FF__bar
梦里兽 2025-01-09 07:34:20

你想要 bin 模式:

perldoc -f binmode
   binmode FILEHANDLE, LAYER
   binmode FILEHANDLE
           Arranges for FILEHANDLE to be read or written in "binary" or
           "text" mode on systems where the run-time libraries distinguish
           between binary and text files.  If FILEHANDLE is an expression,
           the value is taken as the name of the filehandle.  Returns true
           on success, otherwise it returns "undef" and sets $! (errno).

           On some systems (in general, DOS and Windows-based systems)
           binmode() is necessary when you're not working with a text
           file.

You want binmode:

perldoc -f binmode
   binmode FILEHANDLE, LAYER
   binmode FILEHANDLE
           Arranges for FILEHANDLE to be read or written in "binary" or
           "text" mode on systems where the run-time libraries distinguish
           between binary and text files.  If FILEHANDLE is an expression,
           the value is taken as the name of the filehandle.  Returns true
           on success, otherwise it returns "undef" and sets $! (errno).

           On some systems (in general, DOS and Windows-based systems)
           binmode() is necessary when you're not working with a text
           file.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文