0
votes

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.

2
Your request is simple in most computer languages. You'd have a harder time learning how to convert the source code into something your computer could execute. Could you find out what computer language(s) are available on your machine, so you wouldn't have to add a computer language to your computer? - Gilbert Le Blanc
Well, I use windows 7 and windows server 2008 R2. I installed the powershell feature on 2008 and I also have used vb scripts / batch files before. I don't know if it's possible to do what I seek with this, as it would be the easiest option for me to figure out :) Thanks! - user1271818
I was thinking of computer languages like Java or C++. With either computer language, you would have to install an interpreter or a compiler in order for your Windows computer to execute the source code. It might be possible to do what you want in a Visual Basic script, but that's not an area where I have expertise. - Gilbert Le Blanc
I think I'll be able to work my way around installing a compiler if you can share your expertise in C++. Thank you so much! - user1271818

2 Answers

1
votes

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
0
votes

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