troubleshooting Question

Need to split up a large patterned text file into multiple text files.

Avatar of Chad Killion
Chad Killion asked on
PowershellDNS
10 Comments1 Solution85 ViewsLast Modified:
Hello,

I have a 5200 line text file full of DNS zone information.  It is formatted such with line 1 being the SOA record, and the last 2 lines of the zone are "Success..." and then "----" and then it repeats with the next zone.  An example of the top 2 records looks like the following:

domain1.com. SOA dauth5.server.com. contact.domain1.com. 2018020603 10800 3600 604800 10800
webmail.domain1.com. CNAME  domain1.com.
domain1.com. NS  dauth5.server.com.
domain1.com. MX 10 mail3.serverhosting.com.
domain1.com. A  255.255.138.2
mail.domain1.com. CNAME  mail3.serverhosting.com.
pop.domain1.com. CNAME  pop.serverhosting.com.
domain1.com. MX 10 mx1-us1.ppe-hosted.com.
www.domain1.com. CNAME  domain1.com.
domain1.com. NS  dauth6.server.com.
domain1.com. MX 20 mx2-us1.ppe-hosted.com.
smtp.domain1.com. CNAME  smtp.serverhosting.com.
SUCCESS: Getting information for Domain 'domain1.com' complete.
---------------
domain2.com. SOA dauth5.server.com. admin.server.com. 2018052503 900 600 86400 10800
domain2.com. A  192.168.255.2
domain2.com. MX 20 mx2-us1.ppe-hosted.com.
domain2.com. MX 10 mx1-us1.ppe-hosted.com.
www.domain2.com. CNAME  domain2.com.
mail.domain2.com. CNAME  mail4.serverhosting.com.
domain2.com. NS  dauth5.server.com.
domain2.com. NS  dauth6.server.com.
SUCCESS: Getting information for Domain 'domain2.com' complete.
---------------

I would like a powershell script that will take my file as in input, then split the file into multiple text files saved to a specific path.  The file name should be the very first word on line1 (domain1.com.text and domain2.com.text in the scenario above), then they should contain all lines up to the "Success:, and the following------" lines.  Those can be discarded.  So after processing the script given the text file above, I would be left with the following:

c:\temp\domain1.com.txt which would look like:
domain1.com. SOA dauth5.server.com. contact.domain1.com. 2018020603 10800 3600 604800 10800
webmail.domain1.com. CNAME  domain1.com.
domain1.com. NS  dauth5.server.com.
domain1.com. MX 10 mail3.serverhosting.com.
domain1.com. A  255.255.138.2
mail.domain1.com. CNAME  mail3.serverhosting.com.
pop.domain1.com. CNAME  pop.serverhosting.com.
domain1.com. MX 10 mx1-us1.ppe-hosted.com.
www.domain1.com. CNAME  domain1.com.
domain1.com. NS  dauth6.server.com.
domain1.com. MX 20 mx2-us1.ppe-hosted.com.
smtp.domain1.com. CNAME  smtp.serverhosting.com.

and c:\temp\domain2.com.text which would look like:
domain2.com. SOA dauth5.server.com. admin.server.com. 2018052503 900 600 86400 10800
domain2.com. A  192.168.255.2
domain2.com. MX 20 mx2-us1.ppe-hosted.com.
domain2.com. MX 10 mx1-us1.ppe-hosted.com.
www.domain2.com. CNAME  domain2.com.
mail.domain2.com. CNAME  mail4.serverhosting.com.
domain2.com. NS  dauth5.server.com.
domain2.com. NS  dauth6.server.com.

I think I will end up with a few hundred text files, but this would save me a ton of time!  Thanks in advance!
ASKER CERTIFIED SOLUTION
Join our community to see this answer!
Unlock 1 Answer and 10 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 10 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros