Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
I have a file that I need to reformat via either a batch file or VB Script. original sample line of text
"20070301000231","501",65385,21.472,"0008"
"20070510103100","523",65221,19.667,"0002"
I need the new format to be"20070301 000231","501",65385,21.472,"0008"
"20070510 103100","523",65221,19.667,"0002"can a non-delimited field be parsed in a batch file?
thanks...ralph

Between your 2 choices of scripting languages, VB would be best.
I don't work with VB, but here's a Perl commamd line regex solution that can be translated to the VB equivalent.
===================================
perl -pe "s/^(\"\d{8})(\d{6}\")/$1 $2/" file.txt

I don't know VB much, here's one in Python
[code]
for line in open("file"):
line = line.split(",")
print line[0][0:9],line[0][9:]+",",','.join(line[1:])
[/code]
save as script.py and from commandline,
c:\> python script.py > outfilehowever if you also have GNU awk,
[code]
awk 'BEGIN{FS=","}{$1=substr($1,0,9)" "substr($1,9);$0=$0;print} ' file
[/code]

Here's a batch:
@echo off
for /f %%i in (myfile.txt) do (
set line=%%i
call :parser)
goto :eof:parser
set parsed1=%line:~0,9%
set parsed2=%line:~9,33%
echo %parsed1% %parsed2% >>newfile.txt

For the sake of completeness, the VBScript code:
Dim fso, inF, outF, sLine
Set fso = CreateObject("Scripting.FileSystemObject")
Set inF = fso.OpenTextFile("myfile.txt", 1)
Set outF = fso.OpenTextFile("outfile.txt", 2, True)Do Until inF.AtEndOfStream
sLine = inF.ReadLine
outF.WriteLine Left(sLine, 9) & " " _
& Right(sLine, Len(sLine) - 9)
Loop

![]() |
![]() |
![]() |

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.
| Ads by Google |