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
21 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 83

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 83

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 83

Expert Comment

by:oBdA
ID: 41902956
Question answered.
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Suggested Solutions

Set OWA language and time zone in Exchange for individuals, all users or per database.
This article will help you understand what HashTables are and how to use them in PowerShell.
In this tutorial viewers will learn how to position items using CSS's three positioning types Create a new HTML document with an internal stylesheet.: Create another div in CSS and name it Absolute : Type "position:absolute;" and "top:10px; left:50p…
In this tutorial viewers will learn how to embed an audio file in a webpage using HTML5. Ensure your DOCTYPE declaration is set to HTML5: : The declaration should display (CODE) HTML5 is supported by the most recent versions of all major browsers…

747 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now