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
31 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

When crafting your “Why Us” page, there are a plethora of pitfalls to avoid. Follow these five tips, and you’ll be well on your way to creating an effective page.
A quick Powershell script I wrote to find old program installations and check versions of a specific file across the network.
In this tutorial viewers will learn how to embed videos in a webpage using HTML5. Ensure your DOCTYPE declaration is set to HTML5: "<!DOCTYPE html>": Use the <video> tag to insert a video. Define the src as the URL of your video; this is similar to …
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

751 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