header
header Register : : Login header
header
divider
menuleft
menuright
submenu
left

[August 25th, 2008] Check the home page regarding PowerShell related news from a brand new sponsor: Idera

Parsing a Text File
Last Post 22 Jul 2008 01:30 PM by bsonposh. 10 Replies.
Printer Friendly
Sort:
PrevPrev NextNext
You are not authorized to post a reply.
Author Messages
peter.munnellyUser is Offline
New Member
New Member
Posts:1
Avatar

--
21 Jul 2008 10:04 PM  

Hi,

I have a text file that looks similar to the following;

 < Processor >
    Model:                         AMD Sempron(tm) Processor 3400+
    Speed:                         2.00GHz
    Model Number:                  3400 (estimated)
    Cores per Processor:           1 Unit(s)
    Threads per Core:              1 Unit(s)
    Internal Data Cache:           64kB Synchronous, Write-Back, 2-way set, 64
                                   byte line size
    L2 On-board Cache:             256kB ECC Synchronous, Write-Back, 16-way set,
                                    64 byte line size

  < Mainboard >
    Bus(es):                       ISA AGP PCI IMB USB i2c/SMBus
    MP Support:                    1 Processor(s)
    MP APIC:                       Yes
    System BIOS:                   LENOVO 255.23
    System:                        LENOVO 8259JCG
    Mainboard:                     LENOVO K8M800-M3
    Total Memory:                  448MB DDR-SDRAM

  < Chipset 1 >
    Model:                         Legend Ltd K8M400 CPU to PCI Bridge
    Shared Memory:                 1MB

  < Chipset 2 >
    Model:                         Advanced Micro Devices (AMD) Athlon 64 /
                                   Opteron HyperTransport Technology
                                   Configuration

I want to work through the file line by line and extract specific content under the headings that are boxed around < >

For example I want to extract;

< Mainboard >
    MP Support:                    1 Processor(s)
    System BIOS:                   LENOVO 255.23

I then want to continue down the file and extract content from under the next heading.

Does anyone know how this could be done?

Thanks,

 

 

 

bsonposhUser is Offline
Basic Member
Basic Member
Posts:393
Avatar

--
21 Jul 2008 10:16 PM  
I have a blogentry on this

http://bsonposh.com/archives/248
Brandon Shell
----------------
Microsoft Powershell MVP
https://mvp.support.microsoft.com/profile/Brandon
Blog: http://www.bsonposh.com
ssaehrigUser is Offline
New Member
New Member
Posts:31

--
21 Jul 2008 10:50 PM  

I had a similar problem. I used a simple select string statement. Since i only needed the kilobytes string i limited it down. i also out put the results to another file so i could further filter the data down.

gc \path\media*.txt | Select-String kilobytes | Out-File -FilePath c:\txt\tempresults.txt

(this will limit the info down to the strings you need.)

I would try the blog post also I am fairly new to powershell and this community has help me alot so i thought i would give a little back. You will probablty get a better answer from the more experienced members of the community.

Steven Saehrig
MCP, MCTS
halr9000User is Offline
PowerShell MVP, Site Admin
Basic Member
Basic Member
Posts:335
Avatar

--
22 Jul 2008 12:21 AM  
Another note, it will likely be easier to grab the data "first hand" from WMI rather than parsing this text file. Just a thought.

e.g. get-wmiobject wine32_baseboard
Community Director, PowerShellCommunity.org
Co-host, PowerScripting Podcast
Author, TechProsaic
glnsizeUser is Online
Basic Member
Basic Member
Posts:101

--
22 Jul 2008 02:07 AM  
Posted By halr9000 on 07/21/2008 4:21 PM
Another note, it will likely be easier to grab the data "first hand" from WMI rather than parsing this text file. Just a thought.

e.g. get-wmiobject wine32_baseboard

 

I'm with Hal, Way easier… IMO

bsonposhUser is Offline
Basic Member
Basic Member
Posts:393
Avatar

--
22 Jul 2008 02:39 AM  
Posted By glnsize on 07/21/2008 6:07 PM
Posted By halr9000 on 07/21/2008 4:21 PM
Another note, it will likely be easier to grab the data "first hand" from WMI rather than parsing this text file. Just a thought.

e.g. get-wmiobject wine32_baseboard

 

I'm with Hal, Way easier… IMO

 

Assuming that its a Windows Machine w/ the approperiate classes installed, Authentication is in place, and Full TCP connectivety between Point A and Point B, and this information isn't already collected.

Brandon Shell
----------------
Microsoft Powershell MVP
https://mvp.support.microsoft.com/profile/Brandon
Blog: http://www.bsonposh.com
bsonposhUser is Offline
Basic Member
Basic Member
Posts:393
Avatar

--
22 Jul 2008 02:40 AM  
Oh... and if it is not a laptop

System BIOS: LENOVO 255.23
System: LENOVO 8259JCG
Mainboard: LENOVO K8M800-M3
Total Memory: 448MB DDR-SDRAM
Brandon Shell
----------------
Microsoft Powershell MVP
https://mvp.support.microsoft.com/profile/Brandon
Blog: http://www.bsonposh.com
glnsizeUser is Online
Basic Member
Basic Member
Posts:101

--
22 Jul 2008 06:18 AM  

I guess I just have a different mind set... I will grant points one and two, but three is of no consequence. While it may (debatable) be more work to retrieve the information directly. Once you have that resource you can now use it over and over again.

@ peter.munnelly

Not knowing the whole story as is often the case with forums. I see BSonPosh's point you asked about parsing a text file.. Try something like this...

 

# Borrows Heavily from http://bsonposh.com/archives/248
# Not production tested ... ~glenn
$ErrorActionPreference = "SilentlyContinue"
$File = gc < PATH TO FILE>
$processor = "" | Select-Object Model,Speed,ModelNumber,CoresperProcessor,ThreadsperCore,InternalDataCache,L2OnboardCache
$mainboard = "" | Select-Object Bus,mpsuppport,MPAPIC,BIOS,System,Mainboard,TotalMemory
$Chipset1 = "" | Select-Object Model,SharedMemory
$Chipset2 = "" | Select-Object model

function Processor ($item, $value) {
switch ($item)
{
"Model" {$processor.Model = $value}
"Speed" {$processor.Speed = $value}
"Model Number" {$processor.ModelNumber = $value}
"Cores per Processor" {$processor.CoresperProcessor = $value}
"Threads per Core" {$processor.ThreadsperCore = $value}
"Internal Data Cache" {$processor.InternalDataCache =$value}
"L2 On-board Cache" {$processor.L2OnboardCache = $value}
}
}
function Mainboard ($item, $value){
switch ($item)
{
"Bus(es)" {$mainboard.bus = $value}
"MP Support" {$mainboard.mpsuppport = $value}
"MP APIC" {$mainboard.mpapic = $value}
"System BIOS" {$mainboard.bios = $value}
"System" {$mainboard.system = $value}
"Mainboard" {$mainboard.Mainboard = $value}
"Total Memory" {$mainboard.totalmemory = $value}
}
}
function Chipset1 ($item, $value){
switch ($item)
{
"Model" {$chipset1.Model = $value}
"Shared Memory" {$chipset1.SharedMemory = $value}
}
}
function Chipset2 ($item, $value){
switch ($item)
{
"Model" {$chipset2.model = $value}
}
}

$process = 0
foreach ($line in $file ) {
$parsd = $line.Trim().split([string[]](": "),[system.StringSplitOptions]::RemoveEmptyEntries)
$property = $parsd[ 0 ].Trim()
$value = $parsd[ 1 ].Trim()

switch ($property)
{
"< Processor >" {$process = 1}
"< Mainboard >" {$process = 2}
"< Chipset 1 >" {$process = 3}
"< Chipset 2 >" {$process = 4}
}

switch ($process)
{
1 { Processor $property $value}
2 { Mainboard $property $value}
3 { Chipset1 $property $value}
4 { Chipset2 $property $value}
}

}
$pc = "" | select @{e={$processor};n='processor'}, `
@{e={$mainboard};n='mainboard'}, `
@{e={$Chipset1};n='Chipset1'}, `
@{e={$Chipset2};n='Chipset2'}
Write-Output $pc

@BSonPosh, That works wonderfuly... thank you!

$parsd = $line.Trim().split([string[]](": "),[system.StringSplitOptions]::RemoveEmptyEntries)

~Glenn

bsonposhUser is Offline
Basic Member
Basic Member
Posts:393
Avatar

--
22 Jul 2008 01:17 PM  

@glen

Very nice, and here is a little forum tip... when posting code on these forums... put a spaces between brackets and number so you get this (and it still works.)

[ 0 ]

instead of this

[0]

Brandon Shell
----------------
Microsoft Powershell MVP
https://mvp.support.microsoft.com/profile/Brandon
Blog: http://www.bsonposh.com
glnsizeUser is Online
Basic Member
Basic Member
Posts:101

--
22 Jul 2008 01:27 PM  

Thank you,
 
Much better that :y stuff was aggravating me...


~Glenn

bsonposhUser is Offline
Basic Member
Basic Member
Posts:393
Avatar

--
22 Jul 2008 01:30 PM  
np... took me a few (dozen) post before I figured that out ;)
Brandon Shell
----------------
Microsoft Powershell MVP
https://mvp.support.microsoft.com/profile/Brandon
Blog: http://www.bsonposh.com
You are not authorized to post a reply.

Active Forums 4.1
right
   
footer Sponsored by Quest Software • SAPIEN Technologies • ShellTools, LLC • Microsoft Windows Server 2008 footer
footer