Link to home
Start Free TrialLog in
Avatar of Chad K
Chad K

asked on

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

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!
Avatar of oBdA
oBdA

"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

Avatar of Chad K

ASKER

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.
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?
Avatar of Chad K

ASKER

no, the . needs to be added to the end of the domain from line 11 each time.
ASKER CERTIFIED SOLUTION
Avatar of oBdA
oBdA

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Chad K

ASKER

Awesome!  Thanks!