#!/usr/bin/env ruby ############################################################################### # COPYRIGHT 2006 Eric Davis ("eric" + "at.to_sym" + "theadmin.org") ############################################################################### # # Script to split a single text file of Typo posts into multiple text files. # # USAGE: Enter the name of the file to split as the first and only arguement # (extras will be discarded) # # INPUT: Single unix file that has each post delimited by 10 #'s (##########) # # OUTPUT: a directory "split/" that will have each post as a separte file in # it. Filenames are the title of the next line after the 10*#'s but in # lowercase, spaces turned into _, /'s turned into -'s, and .txt appended # # To get the proper input script/runner may be used as follows: # # script/runner -e production "Content.find(:all, :conditions => \ # 'type = \"Article\"').each {|i| puts '##########'; puts i.title;\ # puts ''; puts i.body; puts '' }" > output.txt # ############################################################################### if ARGV.length < 1 puts "You must enter the name of the file to split" exit end require 'fileutils' # Make a folder split FileUtils.mkdir_p 'split' FileUtils.cd 'split' # Read the todo from the origonal folder file = IO.readlines("../#{ARGV[0]}") # Use a tmp variable so I can skip the first close first_run = true # Use this to track if a new file is needed new_file = false f = String.new posts = 0 file.each do |line| if line =~ /^[#]{10,10}$/ f.close if not :first_run new_file = true else if new_file == true posts += 1 new_file_name = line.strip.gsub(/ /, '_').gsub(/\//, '-').downcase + '.txt' f = File.new(new_file_name, "w") first_run = false new_file = false end f.puts line end end f.close puts "Converted #{posts} posts"