regex - Trim extra pipes off end of text file in Windows -
so have process outputs text file pipe delimited, looks this:
|abc123|1*|004|**gobbligook|001|%|2014-01-01||||||||||||| this example, , i'm not sure if answer involves regular expressions. if put actual line. anyways
issue
so example import process accepts file looking 8 pipes, there 20, if sees more pipes after 8 it's looking import process fails.
question
is there process can use in windows environment trim trailing pipes off end of entire file?
update
magoo supplied me great answer working keep getting error: delimiter unexpected @ time
here code:
@echo off setlocal set "sourcedir=c:\users\desktop\pipe delimiter project" set "destdir=c:\users\desktop\pipe delimiter project" ( /f "tokens=1-7delims=|" %%a in ('type "%sourcedir%\test.txt"') ( echo(^|%%a^|%%b^|%%c^|%%d^|%%e^|%%f^|%%g^| ) )>%destdir%\newfile.txt anyone know what's wrong? put in line question |abc123|..| pasted in file 6 times...thanks!
@echo off setlocal enabledelayedexpansion set "sourcedir=." set "destdir=u:\destdir" ( /f "delims=" %%a in ('type "%sourcedir%\q22863616.txt"') ( set "line=%%a" echo(!line:~0,-12! ) )>%destdir%\newfile.txt goto :eof i used file named q22863616.txt containing data testing. produces newfile.txt
assuming final 12 fields empty, lack of information otherwise.
another form, given additional information
@echo off setlocal set "sourcedir=." set "destdir=u:\destdir" ( /f "tokens=1-7delims=|" %%a in ('type "%sourcedir%\q22863616.txt"') ( echo(^|%%a^|%%b^|%%c^|%%d^|%%e^|%%f^|%%g^| ) )>%destdir%\newfile.txt ok - third time's charm.
@echo off setlocal enabledelayedexpansion set "sourcedir=." set "destdir=u:\destdir" :: remove variables starting $ /f "delims==" %%a in ('set $ 2^>nul') set "%%a=" ( /f "delims=" %%a in ('type "%sourcedir%\q22863616.txt"') ( set "$0=%%a" set "$1=%%a" /l %%c in (1,1,8) set "$1=!$1:*|=!" set "$2=%%a" set "$3=" set /a tot=0 /f "delims=:" %%e in ('set $^|findstr /o /r "$"') set /a tot=%%e - !tot! - 5 call :show !tot! call echo %%$2:~0,-!tot!%% ) )>%destdir%\newfile.txt goto :eof :show call set "$3=%%$2:~0,-%1%%" /f "tokens=1*delims==" %%y in ('set $3') echo(%%z goto :eof this seems immune % in data, chokes on ! or &. pays money, takes choice...
Comments
Post a Comment