2012-02-01 47 views
0

我目前在WinXP上运行Strawberry Perl,我正在尝试处理一个unix格式的平面文件。平面文件使用换行符来分隔字段,并使用供稿字符来分隔记录。我试图将FF转换为其他任何东西(CRLF,';',TAB等)。我曾尝试使用下面的Perl的俏皮话,但没有成功尝试:为什么Strawberry 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新手,所以也许我错过了一些东西?有谁知道为什么上述命令不能做我想让他们做的事情?

+0

这可能是我的问题的重要组成部分。我只是试着用双引号,而s ///命令实际上取代了FF字符!然而,它仍然不能满足我需要的功能,我认为外星生命形式的使用binmode()的建议可能是解决方案的其他部分。 – 2012-02-01 18:39:43

回答

8

的问题是,Windows外壳不解释单引号中的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 
+0

。 'perl -pe s/\ f/__ FF __ /'根本没有引号也可以。 – mob 2012-02-01 18:34:50

+0

如何使用输入文本文件使用binmode?使用双引号允许命令实际工作(替换烦人的\ f),但是我仍然将所有\ n转换为\ r \ n。 – 2012-02-01 18:45:02

+1

只需添加'binmode STDOUT'调用就足以取消'\ r \ n'行为:'perl -pe“binmode STDOUT; s/\ f/\ n/g” – mob 2012-02-01 18:58:04

2

你想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. 
+0

Binmode会阻止在窗口上将'\ n'转换为'\ r \ n',但它不会帮助匹配'\ f'。 – 2012-02-01 18:10:53

+1

我很难找到使用binmode单线程的语法示例。我的上面的例子是什么样的binmode?对于没有空格或文件名通配符的命令,根本没有引号或 – 2012-02-01 18:24:22

相关问题