Solved

Read a text file and generate  html one having tabular format in powershell using regex and pattern matching

Posted on 2016-11-06
4
30 Views
Last Modified: 2016-11-27
Below mentioned is  a text of text file

ServeName is STAETL1003Q

DomainName is INFA_DOMAIN_STAETL1003Q

PortName is 6500

ServeName is STAETL1003Qa

DomainName is INFA_DOMAIN_STAETL1003Qa

PortName is 6500

ServeName is STAETL1003Qb

DomainName is INFA_DOMAIN_STAETL1003Qb

PortName is 6501
0
Comment
Question by:Tanu Sidana
  • 3
4 Comments
 
LVL 84

Expert Comment

by:oBdA
ID: 41876671
$TxtFile = 'C:\Temp\temp.txt'
$HtmlFile = 'C:\Temp\temp.html'
$Properties = [ordered]@{}
Get-Content $TxtFile | ForEach-Object {
	If ($_ -match '\A(?<Key>\S+)\s+is\s+(?<Value>.+)\Z') {
		$Properties[$Matches['Key'].Trim()] = $Matches['Value'].Trim()
		If ($Matches['Key'] -eq 'PortName') {
			New-Object -TypeName PSObject -Property $Properties
			$Properties.Clear()
		}
	}
} | ConvertTo-Html | Set-Content -Path $HtmlFile

Open in new window

0
 

Author Comment

by:Tanu Sidana
ID: 41876745
Can you please explain me the logic and i need table borders too
0
 
LVL 84

Accepted Solution

by:
oBdA earned 500 total points (awarded by participants)
ID: 41876756
The script reads the content of the input file, checks for lines matching "<Key> is <Value>".
The results will be stored in a hashtable until a key named "PortName" (the last element in a block) is found, and then written to the pipeline, from where they will be picked up by ConvertTo-Html and finally Set-Content.
$TxtFile = 'C:\PS\temp.txt'
$HtmlFile = 'C:\PS\temp.html'
$HtmlHead = @'
<style>
	TABLE{border-width:1px; border-style:solid; border-color:black;}
	TH{border-width:1px; padding:1px; border-style:solid; border-color:black;}
	TD{border-width:1px; padding:1px; border-style:solid; border-color:black;}
</style>
'@

$Properties = [ordered]@{}
Get-Content $TxtFile | ForEach-Object {
	If ($_ -match '\A(?<Key>\S+)\s+is\s+(?<Value>.+)\Z') {
		$Properties[$Matches['Key'].Trim()] = $Matches['Value'].Trim()
		If ($Matches['Key'] -eq 'PortName') {
			New-Object -TypeName PSObject -Property $Properties
			$Properties.Clear()
		}
	}
} | ConvertTo-Html -Head $HtmlHead | Set-Content -Path $HtmlFile

Open in new window

0
 
LVL 84

Expert Comment

by:oBdA
ID: 41902956
Question answered.
0

Featured Post

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article discusses four methods for overlaying images in a container on a web page
A project that enables an administrator to perform actions within a user session context not just at the time of login but any time later on day(s) or week(s) later.
In this tutorial viewers will learn how to style transparent/translucent elements using alpha transparency in CSS Start with a normal styled element, such as a div.: Define its "background-color" property as "rgba (255, 255, 255, .5): The numbers in…
The viewer will the learn the benefit of plain text editors and code an HTML5 based template for use in further tutorials.

821 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question