user1271818
user1271818

Reputation: 11

Create multiple text files from a single text file with specific filenames

I have a huge text file that I would like to split into multiple files. The data to put in these multiple files, as well as the filenames for these files is in the source content. Here's a sample of the data that goes on forever:

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
...

The filename of the first output file would be W1M0130.txt and the content would be the lines below, down to the next filename (W1M0200). If it can help, the filenames all start with W, and the content lines all start with a date except the last line that is always $END.

Upvotes: 0

Views: 6148

Answers (2)

user1271818
user1271818

Reputation: 11

Here's the solution I ended up with in VBScript. Thank you for your help to the ones who contributed.

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

Upvotes: 1

Joe23
Joe23

Reputation: 5782

Here a Clojure version of what you require. data.txtwould have to be the path to your file.

(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)))))))

Upvotes: 0

Related Questions