Convert Newlines from Windows to Unix

Posted On Wed, 24 Oct 2012

Filed under Batch
Tags: , , , ,

Comments Dropped leave a response

16-Apr-2013: Updated source code. Rewrite of post.

Everybody knows the old trick of converting a text file with Unix newlines (LF) to Windows line-endings (CR+LF):

more unix.txt > win.txt

(Note that more will wait for a keypress after scrolling 65,534 lines, even if output is redirected to a pipe or file.) But converting from Windows to Unix is a far more complicated affair. After searching failed to find any straightforward Batch solutions—apart from this meandering thread on DosTips—I cranked out win2unix.cmd as outlined below.

continue reading »


Ensure Text Files End with a Newline

Posted On Thu, 4 Oct 2012

Filed under Batch
Tags: , , , ,

Comments Dropped leave a response

From time to time, you may find yourself building up a file comprised of command output and snippets from other files. On these occasions, you will no doubt be using pipes and redirection, and commands such as copy, findstr, and type.

Which is all well and good except that you could easily run into trouble if some of the bits and pieces you're cobbling together end with a newline while others don't. For instance, type will join the last line of a file that doesn't end with a newline with the first line from the next file, forming one long line. Probably not what you want.

And according to Dave Benham in his treatise on the Undocumented Features and Limitations of FindStr, findstr under XP and Win7 will hang if redirected input doesn't end with a linefeed character.

Read the rest of this post »