I have a file (1.8 Mb) that has 1 single (very long) row of text. The values on that row are generally separated by 13 blank spaces. What I am trying to do is to replace these 13 blank spaces with a pipe | delimiter so that I can process this text file using SSIS.
So far, I have had no success in programmatically processing this file using a batch file.
I have tried using the below code that I got from another SO post.
@echo off
REM create empty file:
break>R1.txt
setlocal enabledelayedexpansion
REM prevent empty lines by adding line numbers (find /v /n "")
REM parse the file, taking the second token (*, %%b) with delimiters
REM ] (to eliminate line numbers) and space (to eliminate leading spaces)
for /f "tokens=1,* delims=] " %%a in ('find /v /n "" ^<PXZP_SND_XZ01_GFT10553.dat') do (
call :sub1 "%%b"
REM write the string without quotes:
REM removing the qoutes from the string would make the special chars poisonous again
>>PXZP_SND_XZ01_GFT10553.dat echo(!s:"=!
)
REM Show the written file:
type PXZP_SND_XZ01_GFT10553.dat
goto :eof
:sub1
set S=%*
REM do 13 times (adapt to your Needs):
for /l %%i in (1,1,13) do (
REM replace "space qoute" with "quote" (= removing the last space
set S=!S: "=|!
)
goto :eof
Can someone help me here? Example of my text file:
96859471/971 AAAA HAWAII 96860471/971 BBBB HAWAII 96861471/971 CCCC HAWAII 96863471/971 DDDD HAWAII
Use appropiate tools.
To use
Using Regular expressions to replace 2 or more spaces with a bar.
There are two other ways to handle this.
Like the first way is to
replace
multiple times from the longest to shortest number of predifined spaces. IE 13, 10, 8 or 5 spaces.Split
the sting on 2 spaces.Filter
the array to exclude blank array elements. ThenJoin
the array with|
as the delimiter.The
for /F
loop cannot handle lines longer than about 8190 characters. However, there is a way to read files with longer lines: usingset /P
in a loop, together with input redirection<
;set /P
reads at most 1023 characters, unless a line-break or the end of the file is encountered; executing it multiple times for the same open (input-redirected) file handle allows to read very long lines in portions of 1023 characters sinceset /P
does not reset the file pointer.Another challenge is to return (echo) very long lines, which is not possible with the
echo
command again because of the line limitation of about 8190 characters (which applies to command lines and variable contents). Also here block-wise processing helps: firstly, get an end-of-file character (EOF, ASCII 0x1A); then take a text/string portion, append an EOF and write the result to a temporary file usingecho
(which appends a line-break), together with output redirection>
; next copy the file onto itself usingcopy
, but read it in ASCII text mode to discard the EOF and everything after (hence the line-break previously appended byecho
) and write it in binary mode to get an exact copy of the resulting data; lastly, type out the file content usingtype
.The following script makes use of these techniques (see all the explanatory
rem
remarks in the code):The following restrictions exist:
!
,*
,~
and must not contain=
;!
;