windows - Batch file to process csv document to add space in postcode field -
i have csv file populated name, address, , postcode. large number of postcodes not have required space in between e.g lu79gh should lu7 9gh , w13tp should w1 3tp. need add space in each postcode field if not there already, space should before last 3 characters.
what best way solve via windows command line? many thanks
you can for /f
follows:
@echo off setlocal enabledelayedexpansion if "%~1" equ "" (echo.%~0: usage: missing file name.& exit /b 1) if "%~2" neq "" (echo.%~0: usage: many arguments.& exit /b 1) /f %%i in (%~1) (echo.%%i& goto :afterheader) :afterheader /f "skip=1 tokens=1-3 delims=," %%i in (%~1) ( set name=%%i set address=%%j set postcode=%%k set postcode=!postcode: =! echo.!name!,!address!,!postcode:~0,-3! !postcode:~-3! ) exit /b 0
demo:
> type data.csv name,address,postcode n1,a1,lu79gh n2,a2,w13tp n1,a1,lu7 9gh n2,a2,w1 3tp > .\add-space.bat data.csv name,address,postcode n1,a1,lu7 9gh n2,a2,w1 3tp n1,a1,lu7 9gh n2,a2,w1 3tp
you can redirect output file capture it. (but can't redirect same file input, because redirection overwrite input file before can read script. if want overwrite original file, can redirect output new file, , move new file on original after script has finished.)
Comments
Post a Comment