【问题标题】:Create multiple text files from a single text file with specific filenames从具有特定文件名的单个文本文件创建多个文本文件
【发布时间】:2012-04-01 03:34:39
【问题描述】:

早安,

我正在寻找一些代码来帮助我不要在这上面花费 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。

感谢您的帮助。 谢谢!

【问题讨论】:

  • 您的请求在大多数计算机语言中都很简单。您将很难学习如何将源代码转换为您的计算机可以执行的东西。您能否找出您的计算机上可用的计算机语言,这样您就不必在计算机上添加计算机语言?
  • 好吧,我使用的是 windows 7 和 windows server 2008 R2。我在 2008 年安装了 powershell 功能,之前也使用过 vb 脚本/批处理文件。我不知道是否有可能用这个来做我所寻求的,因为这对我来说是最简单的选择:) 谢谢!
  • 我在考虑 Java 或 C++ 等计算机语言。使用任何一种计算机语言,您都必须安装解释器或编译器,以便您的 Windows 计算机执行源代码。在 Visual Basic 脚本中也许可以做你想做的事,但这不是我擅长的领域。
  • 如果您可以分享您在 C++ 方面的专业知识,我想我将能够安装编译器。非常感谢!

标签: vbscript split text-files


【解决方案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

【讨论】:

    【解决方案2】:

    这里是您需要的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)))))))
    

    【讨论】:

    • 谢谢 Joe23。直到现在我才听说过 Clojure :) 我会看看我能挖掘出什么来完成这项工作。干杯!
    • @user1271818 你提到你在 Windows 7 上。所以你可能想要 Windows-Linebreaks \r\n 而不是 Unix 的 \n
    • 嗨。我只是不明白如何使这项工作。我尝试安装 ClojureBox 进行编译,但我无法理解。也许有人有VB脚本解决方案? :)
    猜你喜欢
    • 1970-01-01
    • 2021-09-21
    • 2018-04-08
    • 2015-01-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-14
    • 1970-01-01
    相关资源
    最近更新 更多