update to win2unix
Posted: 22 Oct 2012 08:46
Hello All!
Everyone knows this old trick of converting Unix text files to Windows format:
Btw, more waits for keypress after scrolling 65,534 lines no matter what. But going the other way (from Windows to Unix) is less straightforward. But with a little chicanery and a lot of findstr, I was able to do it.
Type ‘win2unix /?’ for basic usage info. Read the notes for limitations on use.
Read this blog post for more information.
- SB
Everyone knows this old trick of converting Unix text files to Windows format:
Code: Select all
more unix.txt > win.txt
Btw, more waits for keypress after scrolling 65,534 lines no matter what. But going the other way (from Windows to Unix) is less straightforward. But with a little chicanery and a lot of findstr, I was able to do it.
Type ‘win2unix /?’ for basic usage info. Read the notes for limitations on use.
Code: Select all
@echo off & setlocal enableextensions
if "%~1" neq "/re-enter" goto init
shift /1
if "%~1" neq "" (call :win2unix "%~1") else call :win2unix
goto end
:init
(set lf=^
)
set nl=^^^%lf%%lf%^%lf%%lf%
for /f %%h in (^"/?%nl%/h%nl%/he%nl%/hel%nl%/help^") ^
do if /i "%~1"=="%%h" call :usage && goto end
if "%~2" neq "" (>&2 echo(too many arguments& (call) & goto end)
(call;)
setlocal enabledelayedexpansion
if /i "!cmdcmdline!" neq "!cmdcmdline:%comspec% /s /d /c=!" ^
set "piped=1"
endlocal & set "piped=%piped%"
if "%~1" neq "" (if exist "%~1\" (>&2 echo("%~1" is a folder
(call) & goto end) else if not exist "%~1" (
>&2 echo(file "%~1" not found& (call) & goto end)
echo("%~1" | findstr "\* \?" >nul && (
>&2 echo(wildcards (* and ?^) not permitted& (call) & goto end)
if "%~z1"=="0" (>&2 echo(file "%~1" is empty& (call) & goto end)
if defined piped (
>&2 echo(specify input from pipe OR file--but not both& (call)
goto end)) else if not defined piped call :usage && goto end
call "%~dpf0" /re-enter "%~1" | findstr /v "^$"
:end
endlocal & goto :EOF
:win2unix
setlocal
if "%~1"=="" (set "file=") else set "file= "%~1""
for /f "delims=" %%i in ('findstr /n "^"%file%') do (
set "line=%%i"
setlocal enabledelayedexpansion
set "line=!line:*:=!!lf!"
echo(!line!
endlocal
)
endlocal & exit /b 0
:usage
set ^"\n=^^^%lf%%lf%^%lf%%lf%^^"
cls & echo(Converts newlines from Windows to Unix.%nl%%\n%
Usage:%nl%%\n%
%~n0 win.txt [^> unix.txt]%\n%
%~n0 win.txt [^| command-name]%\n%
command-name ^| %~n0 [^> unix.txt]%\n%
command-name1 ^| %~n0 [^| command-name2]%nl%%\n%
where win.txt has ^<CR^>^<LF^> line-endings and unix.txt ^
uses ^<LF^> for end-of-line.%nl%%\n%
Notes:%\n%
- Writes to Standard Output ^(STDOUT^) by default.%\n%
- Input should be 8-bit ASCII.%\n%
- Null Character (ASCII 0) in input will corrupt output.%\n%
- Cannot process lines longer than approx 8kb.
exit /b 0
Read this blog post for more information.
- SB