|
Answer» Good Day
I have a text file with random dates that are formatted like so
, 01 02 94 , 04 06 97 , 23 12 89
etc
I would like a way to have a dos BATCH seach for the dates and replace with the following
,01-02-94 ,04-06-97 ,23-12-89
There is a space between the , and the first number. There are also lots of numbers in the text file besides these. So I need to search for the entire 9 or 10 characters. The number are random. The , and spaces are always the same.
I can do it in microsoft word with wild card searches with the following commands
Word Find (,)( )([0-9])([0-9])( )([0-9])([0-9])( )([0-9])
Replace \1\3\4-\6\7-\9
BUt I need to be able to do it with a dos batch for a vbs script in windows.
Thank You Are the dates always in the same place in each line? Is the text file formatted as CSV? Some examples of full LINES would be helpful.
Do you want every sequence of a COMMA + space + 3 number pairs separated by spaces treated as you have shown e.g. , 01 02 85 to ,01-02-85?
Examples
*Q,08 87,123.90,517, 01 07 98, 139.80,810 12 H,08 87,-123.90,0, 01 07 98, 139.80,810 12 *Q,09 88,34.50,517, 26 09 99, 142.90,710 34 H,09 88,-34.50,0, 26 09 99, 142.90,710 34 *Q,19 88,1232.50,517, 14 09 00, 1142.90,42010 56 H,19 88,-1232.50,0, 14 09 00, 1142.90,42010 56
the file is comma delimited. It is exactly the same amount of comma before the dates. But as you can see sometime it is more or less spaces away from start of line.
Hopes this helps
I would like it to look like
*Q,08 87,123.90,517,01-07-98, 139.80,810 12 H,08 87,-123.90,0,01-07-98, 139.80,810 12 *Q,09 88,34.50,517,26-09-99, 142.90,710 34 H,09 88,-34.50,0,26-09-99, 142.90,710 34 *Q,19 88,1232.50,517,14-09-00, 1142.90,42010 56 H,19 88,-1232.50,0,14-09-00, 1142.90,42010 56The date you wish to reformat is the 5th field in a comma delimited string, therefore.
This looks feasible. However, it is now 22:40 in my time zone, (my bedtime) and I have a job so expect more in 18-20 hours approx. Others may be able to respond before then.
I created a vbs script that can search and display the results
Set objRegEx = CreateObject("VBScript.RegExp")
objRegEx.Global = True objRegEx.Pattern = ", (\d{2}) (\d{2}) (\d{2})"
strSearchString = "*Q,08 87,123.90,517, 01 07 98, 139.80,810 12" strNewString = objRegEx.Replace _ (strSearchString, ",$1-$2-$3")
Wscript.Echo strNewString
How do i get this to read every line of a text file and replace it.
File name write.vbs
Const ForReading = 1 Dim objLog, objFile, objHost Set objFSO = CreateObject("Scripting.FileSystemObject") Set objHost = objFSO.OpenTextFile("test.txt", ForReading) Set objLog = objFSO.OpenTextFile("test2.txt", 2, True)
Set objRegEx = CreateObject("VBScript.RegExp")
objRegEx.Global = True objRegEx.Pattern = ", (\d{2}) (\d{2}) (\d{2})"
Do Until objHost.AtEndOfStream
strSearchString = objHost.ReadLine strNewString = objRegEx.Replace _ (strSearchString, ",$1-$2-$3")
Set colMatches = objRegEx.Execute(strSearchString)
If colMatches.Count > 0 Then
For Each strMatch in colMatches
objLog.WriteLine strNewString
Next
End If
Loop
objHost.Close
This will read the first file and write to the second. It does what I need but it deletes any lines that dont have the matching values. Does anyone know how to keep the non matching values also. This uses a native Windows batch script called Jrepl.bat written by dbenham http://www.dostips.com/forum/viewtopic.php?f=3&t=6044
Place Jrepl.bat in the same folder as the batch file, or in a folder that is on the SYSTEM path.
There is also copy on Dropbox (unblock it after downloading): https://www.dropbox.com/s/4otci4d4s8x5ni4/Jrepl.bat
Code: [Select]@echo off call jrepl ", ([0-9][0-9]) ([0-9][0-9]) ([0-9][0-9])," ", $1-$2-$3," /f "file.csv" /o - pause Thank You
This works perfectly
|