1

次の情報を含む csv ファイルがあります。

OS Name:                   Microsoft© Windows Server© 2008 Standard 
OS Version:                6.0.6002 Service Pack 2 Build 6002
System Manufacturer:       IBM
System Model:              IBM 3850 M2 / x3950 M2 -[7233Z1H]-

表示を読み取ってフォーマットするには、powrshell を使用する必要があります。既存のコードは次のとおりです。

$Array = @()
Get-Content <txtfilename> | foreach {
$Test = $_
$Title = $Test.split(":")[0]
$Content = $Test.split(":")[1]
$Obj = New-Object System.Object
$Obj | Add-Member -MemberType NoteProperty -value $Title -Name Title
$Obj | Add-Member -MemberType NoteProperty -value $Content -Name Value
$Array += $Obj
}

$Array | Select Title,Value | Export-Csv <csvfilename> -NoTypeInformation

次の情報が表示されます。

Title      Content
OS Name                         Microsoft? Windows Server? 2008 Standard 
OS Version                      6.0.6002 Service Pack 2 Build 6002
System Manufacturer             IBM
System Model                    IBM 3850 M2 / x3950 M2 -[7233Z1H]-

次のように 1 行で表示される情報が必要です。

ServerName, OS Name , OS Version,  System Manufacture, Sytem model,
TestServer1, Microsoft Windows, 6.0.6002 Service Pack 2, IBM, IBM 3850

ありがとう

4

3 に答える 3

0

まず、簡単な修正です。あなたはCSVを持っていると言います。フォーマットされたテキストファイルがあり、それからCSVを作成したいようです。

その仮定に続いて、このような何かがあなたを始めるはずです。テキストドキュメントには、これらの4つのアイテムのみが含まれているか、これらの4つのアイテムが複数回繰り返されていることを前提としています。常に同じ順序で、間にスペースはありません。テキストファイルの再フォーマットは、それらがどのように見えるかについての例がたくさんなければ、決して楽しいことではありません。

そうは言っても、次のことがあなたを始めるはずです。

[int]$i = 1
$outString = ""
Get-Content C:\users\chris\desktop\info.txt | % {
    #Take the line, remove the title, remove whitespaces from inside of the line and trim whitespaces from the outside.
    $lineContent = ($_.split(':')[1] -replace '\s+', ' ').Trim()
    #Chunk them together...
    if ($i -lt 4){
        $outstring += $linecontent + ','
        $i += 1
    }
    #or insert a return, new line
    else{
        $outstring += $linecontent + "`r`n"
        $i = 1
    }
}
#Add a header row.  Now $outstring is your CSV formatted text.
$outstring = "OSName,OSVersion,SystemMfg,SystemModel`r`n"+$outstring
于 2012-12-10T14:14:37.053 に答える
0

これは、独自のヘッダーがいくつあるのか、それらが何と呼ばれているのかわからない、より高度なケースに対する 2 つ目の回答です。それらが一貫して同じで同じ順序である限り、次のように動作します。

$filePath = "C:\users\chris\desktop\info.txt"
$headers = @()
[int]$headerCount = 0
$endUnique = $false
Get-Content $filePath | % {
    $header = ($_.split(':')[0] -replace '\s+', ' ').Trim()
    if (($headers -notcontains $header) -and (!$endUnique)){
        $headers += $header
        $headerCount += 1
    }
    else{
        $endUnique = $true
    }
}
$headerRow = ""
$headers | % {$headerRow += $_ + ","}
$headerRow = $headerRow.Substring(0,$headerRow.Length-1)

[int]$i = 1
$outString = ""
Get-Content $filePath | % {
    $lineContent = ($_.split(':')[1] -replace '\s+', ' ').Trim()
    if ($i -lt $headerCount){
        $outstring += $linecontent + ','
        $i += 1
    }
    else{
        $outstring += $linecontent + "`r`n"
        $i = 1
    }
}
$outstring = $headerRow+ "`r`n"+$outstring

より高度な PowerShell ユーザーからの他の回答を確認したいと思います。

于 2012-12-10T15:50:12.730 に答える