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.