<http://www.netikka.net/tsneti/info/tscmd139.htm>
Copyright © 2003- by Prof. Timo Salmi  
Last modified Sun 29-Jan-2017 19:54:36

 
Assorted NT/2000/XP/.. CMD.EXE Script Tricks
From the html version of the tscmd.zip 1cmdfaq.txt file
To the Description and the Index
 

This page is edited from the 1cmdfaq.txt faq-file contained in my tscmd.zip command line interface (CLI) collection. That zipped file has much additional material, including a number of detached .cmd script files. It is recommended that you also get the zipped version as a companion.

Please see "The Description and the Index page" for the conditions of usage and other such information.



139} How do I convert UNIX newlines to PC eolns? And vice versa?

The PC eoln (end of line) is an ASCII 13 10 pair.
The UNIX eoln is ASCII 10.


Using SED:
  @echo off & setlocal enableextensions
  rem UNIX newlines to PC eolns
  set source_=C:\_M\UNIX.TXT
  set target_=C:\_M\PC.TXT
  sed -e "s/\x0a/\x0d\x0a/" %source_% > %target_%
  endlocal & goto :EOF

C:\_M\UNIX.TXT


C:\_M\PC.TXT


Written in a more "filename, handle and program version tolerant" way
  @echo off & setlocal enableextensions
  rem UNIX newlines to PC eolns
  set source_=C:\_M\My UNIX test.TXT
  set target_=C:\_M\My PC test.TXT
  < "%source_%" sed -e "s/\x0a/\x0d\x0a/" > "%target_%"
  endlocal & goto :EOF

In fact, any "dummy" sed conversion will do:
  sed -e "s/ / /" < "%source_%" > "%target_%"

Using G(nu)AWK
  @echo off & setlocal enableextensions
  rem UNIX newlines to PC eolns
  set source_=C:\_M\UNIX.TXT
  set target_=C:\_M\PC.TXT
  gawk "BEGIN{RS=\"[\012]\"}{print $0}" "%source_%" > "%target_%"
  endlocal & goto :EOF

Actually, to convert a text file with UNIX newlines to PC eolns no third party utilities are necessary:
  @echo off & setlocal enableextensions
  rem UNIX newlines to PC eolns
  set source_=C:\_M\My UNIX test.TXT
  set target_=C:\_M\My PC test.TXT
  more < "%source_%" > "%target_%"
  endlocal & goto :EOF
or
  @echo off & setlocal enableextensions
  rem UNIX newlines to PC eolns
  set source_=C:\_M\My UNIX test.TXT
  set target_=C:\_M\My PC test.TXT
  find /v "" < "%source_%" > "%target_%"
  endlocal & goto :EOF

The reverse task, PC --> UNIX eolns can be done e.g. as follows
  @echo off & setlocal enableextensions
  rem PC eolns to UNIX newlines
  set source_=C:\_M\My PC test.TXT
  set target_=C:\_M\My UNIX test.TXT
  unxtr -d \015 < "%source_%" > "%target_%"
  endlocal & goto :EOF
Where \015 is octal for ASCII 13 (decimal).

There naturally also is a number of specialized third-party utilities to do the conversion, including
  tsfilt25.zip
  Programs for filtering messages, log files, Unix-PC, etc

The reverse task, i.e. converting PC eolns to UNIX newlines is most naturally done on the UNIX host e.g. with the following Bourne Shell script:
  pc2unix
  Filter PC end of lines to Unix end of lines, T.Salmi

You may also wish to see the items #15 and #163.

References/Comments: (If a Google message link fails try the links within the brackets.)
  Google Groups Apr 28 2006, 3:02 am [M]