Is there a way to convert all CRs to CRLFs in a text file?
When I open a text file from Linux server on Windows, all text is displayed in one line, but actually it's a multi line one.
I'd like to perform the conversion in a batch file.
Can anyone advice, please?
Answer. 2) On Windows or UNIX, gzip and gunzip has a -a switch that tells Windows gunzip to uncompress a file and change all LF's to CRLF. 3) If this is a PGP file, UNIX and Linux has several different utilities that change "UNIX" text files to "Windows" text files.
CR and LF are control characters or bytecode that can be used to mark a line break in a text file. CR = Carriage Return ( \r , 0x0D in hexadecimal, 13 in decimal) — moves the cursor to the beginning of the line without advancing to the next line.
The term CRLF refers to Carriage Return (ASCII 13, \r) Line Feed (ASCII 10, \n). For example: in Windows both a CR and LF are required to note the end of a line, whereas in Linux/UNIX an LF (\n) is only required. Files can be converted from one to another using the . gsub formula.
Unix systems use a single character -- the linefeed -- and Windows systems use both a carriage return and a linefeed (often referred to as "CRLF").
Line separators and line terminators have been a source of compatibility friction between systems as long as there has been more than one kind of system and an urge to exchange data. The Wikipedia article on the Newline has a decent overview of the historical context. And, it suggests a variety of solutions to this problem specifically for use on the Unix side or the Windows side.
On the Unix (Linux) side, look for a utility named unix2dos
and its close relative dos2unix
. These are commonly available, either as a component of a commercial Unix or as open source tools. If available, they are the best answer because they (usually, see your verson's man pages for details) are careful about files that are accidentally written with both line endings. In that unfortunate case, a trip through both utilities will usually clean up the file to be internally consistent. In the absence of these convenient commands, many native utilities can be made to do the conversion. For instance, converting DOS CRLF line endings to Unix newlines can be done with the tr
command:
$ tr -d '\r' < inputfile > outputfile
But do note the caveat that this command assumed that all lines were terminated by CRLF (or LFCR) and works by simply deleting every CR character from the input. Any naked CR characters will be lost.
On the DOS and Windows side, it used to be a lot bleaker. Ports of unix2dos
and dos2unix
certainly exist, for instance they are included in the much larger Cygwin tools that provide a complete unix emulation on a Windows machine. But a solution using only built-in features was hard to find.
Modern Windows (probably since Windows XP), however, is better. There, the built-in FIND command is much less touchy about choice of line terminator than it used to be, and can be used to do the required conversion from Unix line endings to DOS endings. The Wiki page cited above gives this recipe:
C:\...> TYPE filename.u | FIND "" /V >filename.txt
Experimentation shows that this works as well, but it may not give identical results for unknown reasons:
C:\...> FIND "" /V <filename.u >filename.txt
In both cases, you create a copy of the file with the changed line endings. It would probably not be recommended to change the files in place.
I'll mention one other approach that always seems tempting on paper. When you use Samba to provide the file system share on the Linux server for mounting by Windows, there is a configuration option you can set for the share that mounts it in "text mode". Shares mounted in "text mode" automatically have line endings converted. If it works for you, that is probably the cleanest possible solution. Both systems use their preferred text file format, and neither has to fuss about it. But test carefully, this solution is full of edge cases and pitfalls. Most importantly, don't expect binary files on a text mode file system mount point to read correctly. They often will, but not necessarily always.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With