Need to reformat a text file pattern into a different patterned output

Chad Killion
Chad Killion used Ask the Experts™
on
I have a large text file that consists of the top 11 lines of a bunch of dns zone files.  I need to parse this file and create a new file that generates a single line for every iteration of the pattern.  Essentially the pattern consists of 11 lines, followed by a domain line and then repeats.  For example, the pattern looks like the following:

; *** This file is automatically generated by Plesk ***
$TTL	86400

@	IN	SOA	dauth5.domain.com. contact.domain2.com. (
			2018020603	; Serial
			10800	; Refresh
			3600	; Retry
			604800	; Expire
			10800 )	; Minimum

---------------domain2.com

Open in new window


This pattern repeats over and over for every domain name contained on our server.  What I would like is to parse the file for each of these 11 lines and return the following:

domain2.      SOA    dauth5.domain.com.  2018020603 10800 3600 604800 10800

The "domain2" above comes from line 11 each time.  Can anyone write a script or perhaps a quick way in Notepad++ to get the data formatted the way I want?  Thanks!
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Most Valuable Expert 2018
Distinguished Expert 2018

Commented:
"Large text file" and "parse" seem to call for PowerShell.
Do you need the output just space separated, or as fixed column width (and if so, which width for which column?)
How exactly is the domain name generated? Just by cutting of .com, or the element after the last "."?
$inFile = 'C:\Temp\dns.txt'
$outFile = 'C:\Temp\dns_out.txt'
Get-Content -Path $inFile | ForEach-Object {
	Switch -Regex ($_) {
		'@\s+IN\s+\SOA\s+(?<SOA>\S+)' {$props = @{SOA = $Matches['SOA']}}
		'^\s+(?<Serial>\d+)\s*;\s*Serial' {$props['Serial'] = $Matches['Serial']}
		'^\s+(?<Refresh>\d+)\s*;\s*Refresh' {$props['Refresh'] = $Matches['Refresh']}
		'^\s+(?<Retry>\d+)\s*;\s*Retry' {$props['Retry'] = $Matches['Retry']}
		'^\s+(?<Expire>\d+)\s*;\s*Expire' {$props['Expire'] = $Matches['Expire']}
		'^\s+(?<Minimum>\d+)\s*\)\s*;\s*Minimum' {$props['Minimum'] = $Matches['Minimum']}
		'^-+(?<Domain>[^-]\S+)' {
			$props['Domain'] = $Matches['Domain']
			$outObject = New-Object -TypeName PSCustomObject -ArgumentList $props
			$outString = '{0} {1} {2} {3} {4} {5} {6} {7}' -f `
				$($outObject.Domain -replace 'com$'),
				'SOA',
				$outObject.SOA,
				$outObject.Serial,
				$outObject.Refresh,
				$outObject.Retry,
				$outObject.Expire,
				$outObject.Minimum
			$outString | Write-Host
			$outString
		}
	}
} | Set-Content -Path $outFile

Open in new window

Author

Commented:
Sorry, I messed up a bit...that line should be the whole domain.com followed by a "."  This part should be pulled form the line 11 and always follows 15 "-" marks.  Space delimited is just fine as well.
Most Valuable Expert 2018
Distinguished Expert 2018

Commented:
Easy enough to fix, but first another question: does the domain in this line already have a "." at the end (that maybe got edited away), or does the "." need to be explicitly added?
Become a CompTIA Certified Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

Author

Commented:
no, the . needs to be added to the end of the domain from line 11 each time.
Most Valuable Expert 2018
Distinguished Expert 2018
Commented:
Try this then:
$inFile = 'C:\Temp\dns.txt'
$outFile = 'C:\Temp\dns_out.txt'
Get-Content -Path $inFile | ForEach-Object {
	Switch -Regex ($_) {
		'@\s+IN\s+\SOA\s+(?<SOA>\S+)' {$props = @{SOA = $Matches['SOA']}}
		'^\s+(?<Serial>\d+)\s*;\s*Serial' {$props['Serial'] = $Matches['Serial']}
		'^\s+(?<Refresh>\d+)\s*;\s*Refresh' {$props['Refresh'] = $Matches['Refresh']}
		'^\s+(?<Retry>\d+)\s*;\s*Retry' {$props['Retry'] = $Matches['Retry']}
		'^\s+(?<Expire>\d+)\s*;\s*Expire' {$props['Expire'] = $Matches['Expire']}
		'^\s+(?<Minimum>\d+)\s*\)\s*;\s*Minimum' {$props['Minimum'] = $Matches['Minimum']}
		'^-+(?<Domain>[^-]\S+)' {
			$props['Domain'] = $Matches['Domain'] + '.'
			$outObject = New-Object -TypeName PSCustomObject -ArgumentList $props
			$outString = '{0} {1} {2} {3} {4} {5} {6} {7}' -f `
				$outObject.Domain,
				'SOA',
				$outObject.SOA,
				$outObject.Serial,
				$outObject.Refresh,
				$outObject.Retry,
				$outObject.Expire,
				$outObject.Minimum
			$outString | Write-Host
			$outString
		}
	}
} | Set-Content -Path $outFile

Open in new window

Author

Commented:
Awesome!  Thanks!

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial