美好的一天,
我正在寻找一些代码,以帮助我不花3周的时间。我不知道编码,只是搞清楚如何使用这些代码会很有趣。 :)如果有人可以建议一种方法来解决我的问题,我会非常感激。
我有一个巨大的文本文件,我想分成多个文件。放入这些多个文件的数据以及这些文件的文件名都在源内容中。以下是永远存在的数据样本:
W1M0130
03/12/2012 00:00 SS_001 0 0 0 0 0 0 0 0
03/12/2012 00:00 SS_002 15 14 149 64 0 0 0 1
03/12/2012 00:00 SS_003 4 3 233 100 0 0 0 1
03/12/2012 00:00 SS_004 0 0 0 0 0 0 0 0
03/12/2012 00:00 SS_005 0 0 0 0 0 0 0 0
03/12/2012 00:00 SS_006 0 0 0 0 0 0 0 0
03/12/2012 00:00 SS_007 0 0 0 0 0 0 0 0
03/12/2012 00:00 SS_008 0 0 0 0 0 0 0 0
$END
W1M0200
03/12/2012 00:30 SS_001 0 0 0 0 0 0 0 0
03/12/2012 00:30 SS_002 12 11 136 58 0 0 0 1
03/12/2012 00:30 SS_003 3 2 213 91 0 0 0 1
03/12/2012 00:30 SS_004 0 0 0 0 0 0 0 0
03/12/2012 00:30 SS_005 0 0 0 0 0 0 0 0
03/12/2012 00:30 SS_006 0 0 0 0 0 0 0 0
03/12/2012 00:30 SS_007 0 0 0 0 0 0 0 0
03/12/2012 00:30 SS_008 0 0 0 0 0 0 0 0
$END
W1M0230
...
第一个输出文件的文件名为W1M0130.txt,内容为下面的行,直到下一个文件名(W1M0200)。如果它可以提供帮助,文件名都以W开头,内容行都以日期开头,除了最后一行总是$ END。
感谢您的帮助。 谢谢!
答案 0 :(得分:1)
这是我在VBScript中最终得到的解决方案。感谢您对贡献者的帮助。
textFile = "C:\data.txt"
saveTo = "C:\"
writeTo = ""
headingPattern = "(W[0-9][A-Z][0-9]*)"
dim fso,fileFrom,regex
set fso = CreateObject("Scripting.FileSystemObject")
set fileFrom = fso.OpenTextFile(textFile)
set regex = new RegExp
with regex
.Pattern = headingPattern
.IgnoreCase = false
.Global = True
end with
while fileFrom.AtEndOfStream <> true
line = fileFrom.ReadLine
set matches = regex.Execute(line)
if matches.Count > 0 then
writeTo = saveTo & matches(0).SubMatches(0) & ".txt"
set fileTo = fso.CreateTextFile(writeTo)
else
fileTo.WriteLine(line)
end if
wend
set fileFrom = nothing
set fso = nothing
set regex = nothing
答案 1 :(得分:0)
这是您需要的Clojure
版本。 data.txt
必须是您文件的路径。
(def f "data.txt")
(defn is-file [s]
(.startsWith s "W"))
(defn is-end [s]
(= s "$END"))
(defn file-writer [f]
(java.io.FileWriter. f))
(with-open [r (java.io.FileReader. f)
buffered (java.io.BufferedReader. r)]
(loop [l (line-seq buffered)
writer nil]
(when (seq l)
(let [cur-line (first l)
rest-lines (rest l)]
(cond
(is-file cur-line)
(recur rest-lines (file-writer (str cur-line ".txt")))
(is-end cur-line)
(do
(.close writer)
(recur rest-lines nil))
:else
(do
(.write writer (str cur-line "\n"))
(recur rest-lines writer)))))))