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
32 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 85

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 85

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 85

Expert Comment

by:oBdA
ID: 41902956
Question answered.
0

Featured Post

Don't Cry: How Liquid Web is Ensuring Security

WannaCry is just the start. Read how Liquid Web is protecting itself and its customers against new threats.

Question has a verified solution.

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

A recent project that involved parsing Tableau Desktop and Server log files to extract reusable user queries for use in other systems. I chose to use PowerShell to gather the data, and SharePoint to present it...
Originally, this post was published on Monitis Blog, you can check it here . Websites are getting bigger and more complicated by the day. Video, images and custom fonts are all great for showcasing your product or service. But the price to pay in…
In this tutorial viewers will learn how to code links for mobile sites that, once clicked, send a call or text to a specified number. For a telephone link (once clicked, calls a number), begin with a normal "<a href=" link tag. For the href, specify…
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…

635 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