Question

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?

Was it helpful?

Solution

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

OTHER TIPS

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.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top