Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8230935
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T17:18:16+00:00 2026-06-07T17:18:16+00:00

Trying to figure out the best way (either by using what I know in

  • 0

Trying to figure out the best way (either by using what I know in Grep / Sed / Awk) to split up an XML file based on it’s individual string (key?). I have an XML file that is a SQL dump of all my current FAQ entries so it contains an entry ID and then a rather large HTML formatted document. I’m looking to split these entries up so I can easily pop them into an editor and clean up the formatting to import to a new KB / FAQ system. Here’s an example of my data:

 <article id="3">
  <language>en</language>
  <category>Category Name</category>
  <keywords>Keywords, by, comma</keywords>
  <question>Question?</question>
  <answer>HTML Formatting</answer>
  <author>Author</author>
  <data>2010-05-13 09:32</data>
 </article>

The XML file contains every single KB article I have back to back in this format. I am comfortable with bash to figure it out, I just don’t know how to split it into multiple files based on the search.

Cheers,

Clay

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T17:18:17+00:00Added an answer on June 7, 2026 at 5:18 pm

    Use XPath to Extract Articles

    If your file is valid XML, you can use a utility like xgrep or XMLStarlet to parse the file for an XPath expression. For example, using xgrep:

    xgrep -x "//article[@id]" /tmp/foo
    

    This may be all you need. However, it won’t split the articles; it just extracts the correct portions of your XML more reliably than with the use of regular expressions.

    Split Article Nodes into Files with Pipeline

    If you actually need to split the articles into separate files, you can do something like this:

    xgrep -x "//article[@id]" /tmp/foo.rb |
    ruby -ne 'BEGIN { counter=0 }
              counter += 1 if /<article/
              if /<article/ ... /<\/article/
                File.open("#{counter}.xml", "a") { |f| f.puts $_ }
              end'
    

    Obviously, you could do the whole thing with a Ruby XML library, but I prefer treating this sort of problem as a shell pipeline. Your mileage may vary.

    Also, please note that the Ruby script above will number your articles sequentially instead of by article ID. This may be preferable if you have duplicate IDs in your XML.

    Pure Ruby with XmlSimple

    Okay, okay…I just couldn’t leave this one alone. It seemed like a good idea at first to use the external shell utility in a pipeline as above, but if you’re going to use Perl or Ruby anyway, you might as well just use the XmlSimple library.

    • http://metacpan.org/pod/XML::Simple
    • http://xml-simple.rubyforge.org/

    The Ruby script below is a little longer than the pipeline version, but gives you much more control and flexibility. Consider all the possibilities you have with this as a starting point:

    #!/usr/bin/env ruby
    
    require 'xmlsimple'
    
    counter = 0
    node_name = 'article'
    xml = XmlSimple.xml_in '/tmp/foo'
    
    xml[node_name].uniq.each do |node|
      counter = sprintf("%03d", counter.next)
      XmlSimple.xml_out(node,
                        RootName: node_name,
                        OutputFile: "/tmp/#{counter}.xml")
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to figure out the best way to do this...I am using JQuery
I'm implementing jQuery File Upload and trying to figure out the best way to
I'm using Eclipselink and I'm trying figure out the best way to create a
I am trying to figure out the best way to generate an XML sitemap
Trying to figure out the best way to set up collection lists for users
Trying to figure out the best way to do this: Should I do something
I'm trying to figure out the best way to do this, but I'm getting
I'm trying to figure out the best way to store an ArrayList<HashMap<String, String>> item
I'm trying to figure out the best way to create a row of three
I'm trying to figure out the best way to create three 284x87 rounded rectangle

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.