需要从1个非常长的文本文件行中替换13个空格 [英] Need to replace 13 blank spaces from 1 very long line of text file
问题描述
我有一个文件(1.8 Mb),其中有1个单行(很长)的文本行.该行上的值通常由13个空格分隔.我想做的是用管道替换这13个空格.分隔符,以便我可以使用SSIS处理此文本文件.
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.
我尝试使用下面的代码是从另一个SO帖子获得的.
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
推荐答案
使用适当的工具.
Set Inp = wscript.Stdin
Set Outp = wscript.Stdout
Outp.Write Replace(Inp.ReadAll, " ", "|")
要使用
cscript //nologo "C:\Replace13Spaces.vbs" < "c:\folder\inputfile.txt" > "C:\Folder\Outputfile.txt"
使用正则表达式用竖线替换2个或更多空格.
Using Regular expressions to replace 2 or more spaces with a bar.
Set Inp = wscript.Stdin
Set Outp = wscript.Stdout
Set regEx = New RegExp
regEx.Pattern = "\s{2,}"
regEx.IgnoreCase = True
regEx.Global = True
Outp.Write regEx.Replace(Inp.ReadAll, "|")
还有其他两种方法可以处理此问题.
There are two other ways to handle this.
-
类似于第一种方法,是从最长到最短的预定义空间多次
replace
次. IE 13、10、8或5个空格.
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
2个空格上的字符串. Filter
排除空白数组元素的数组.然后Join
以|
作为分隔符的数组.
Split
the sting on 2 spaces. Filter
the array to exclude blank array elements. Then Join
the array with |
as the delimiter.
这篇关于需要从1个非常长的文本文件行中替换13个空格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!