是否有可能按日期对文本文件中的行进行排序并将结果保存到另一个输出文件?每行以日期(DD.MM.YYYY)开头。日期和文本之间的分隔符是Tab(无空格)。我更喜欢VBS中的解决方案。
来源
25.11.1968 Death of Upton Sinclair 14.06.1946 Birthday Donald Trump 25.11.2016 Death of Fidel Castro 14.06.1969 Birthday Steffi Graf 01.01.2017 New Year
到新订单(目标)
01.01.2017 New Year 14.06.1946 Birthday Donald Trump 14.06.1969 Birthday Steffi Graf 25.11.1968 Death of Upton Sinclair 25.11.2016 Death of Fidel Castro
命令更改和比较:月 - 日 - 年
答案 0 :(得分:0)
在这里,它并不漂亮,它没有按照你想要的相反顺序排序,但希望如果你像我一样学习,这会教你一些东西......
Option Explicit
Const fsoForReading = 1
Const fsoForWriting = 2
inputFile = "input.txt"
outputFile = "output.txt"
dim inputFile, outputFile, lineCount, file, fline, lDate, lYear, lMonth, lDay, iDate,output
dim a, d, i
dim objFSO, objFile
Set objFSO = CreateObject("Scripting.FileSystemObject")
set a = CreateObject("System.Collections.ArrayList")
set d = CreateObject("Scripting.Dictionary")
'open up the file for reading
Set objFile = objFSO.GetFile(inputFile)
Set file = objFile.OpenAsTextStream(fsoForReading,-2)
Do Until file.AtEndOfStream
fline = file.ReadLine 'get the line
lDate = left(fline,10) 'get the date portion at the beginning (the first 10 characters)
lYear = split(lDate,".")(2) 'parse the year from the date
lMonth = split(lDate,".")(1) 'parse the month
lDay= split(lDate,".")(0) 'parse the day
iDate = lYear + lMonth + lDay 'put together the "index date". The date needs to be formatted as yyyymmdd so it is sortable
a.add iDate 'add the index date to an array for easier sorting
d.add iDate, fline 'add the index date and line contents to a dictionary object
Loop 'go to the next line in the file
a.Sort() 'sort the array of dates
for each i in a 'loop through the array of dates
output = output + d(i) + vbCrlf 'add the appropriate dictionary object to the output
Next
call writeFile(output) 'write the file
file.Close
WScript.Quit 0
sub writeFile(fc)
dim fso
'fn = fn + ".hl7"
Set fso = CreateObject("Scripting.FileSystemObject")
if (fso.FileExists(outputFile)) then
Set objFile = fso.OpenTextFile(outputFile,8,True)
objFile.writeline fc
objFile.Close
else
Set objFile = fso.CreateTextFile(outputFile,True)
objFile.writeline fc
objFile.Close
end if
end sub