2016-07-21 6 views
0

ディレクトリ内の複数のファイルから文字列を取得しますので、フォルダ内のすべてのファイルに対してこのような行が表示されます。次のファイル。コンテンツを取得し、列とcsvを編集

Host: blah.blah 
OS: "Windows 7" 
"Windows User","bc","btfr1","Internal user for" 
"Windows User","bc3","btfr2","user for" 
"Windows User","bc8","btfr3","user" 
"Windows User","bc2","btfr4","something" 

Host: bleh.bleh 
OS: "Windows 8.1" 
"Windows User","cb5","frtb5","External user for" 
"Windows User","cb2","frtb2","user" 
"Windows User","cb6","frtb1","add files for" 

私はこのようなフォームは次生産することにしたい。

"blah.blah","Windows 7","Windows User","bc","btfr1","Internal user for" 
"blah.blah","Windows 7","Windows User","bc3","btfr2","user for" 
"blah.blah","Windows 7","Windows User","bc8","btfr3","user" 
"blah.blah","Windows 7","Windows User","bc2","btfr4","something" 
"bleh.bleh","Windows 8.1","Windows User","cb5","frtb5","External user for" 
"bleh.bleh","Windows 8.1","Windows User","cb2","frtb2","user" 
"bleh.bleh","Windows 8.1","Windows User","cb6","frtb1","add files for" 

か:

"Windows 7","blah.blah","Windows User","bc","btfr1","Internal user for" 
"Windows 7","blah.blah","Windows User","bc3","btfr2","user for" 
"Windows 7","blah.blah","Windows User","bc8","btfr3","user" 
"Windows 7","blah.blah","Windows User","bc2","btfr4","something" 
"Windows 8.1","bleh.bleh","Windows User","cb5","frtb5","External user for" 
"Windows 8.1","bleh.bleh","Windows User","cb2","frtb2","user" 
"Windows 8.1","bleh.bleh","Windows User","cb6","frtb1","add files for" 

コードを以下に書かれています。

Set-Location c:/mine 
Get-Content *.csv ` 
| Select-String -pattern 'OS|Host|"Windows User"'| Set-Content results.csv 
$text = Get-Content results.csv 

$OutData = foreach ($str in $text) { 
if ($str -match '^Host:\s.*$') { 
    # get prefix 
    $prefix = $str -replace '^Host:\s?(.*)$','$1' 
} 
elseif ([string]::IsNullOrEmpty($str)) { 
    # Do not touch empty strings! 
} 
else { 
    # generate out string with matched prefix 
    $outString = "`"$prefix`",$str" 
    Write-Output $outString 


    } 
} 

$OutData | Out-File results.csv -Force -Encoding utf8 

私は仕事のためにその後の「if」1以上を取得すると考えますが、テーブルには、私はそれを文字列として持つことはできませんので、必ずホスト名に応じて変化する1行を取得します。

答えて

1

このような何か:最後に、それはforeachの内のコードに変更する必要があります行わ

$text = Get-Content *.csv 

$OutData = foreach ($str in $text) { 

    if ($str -match '^Host:\s.*$') { 
     # get prefix 
     $prefix = $str -replace '^Host:\s?(.*)$','$1' 
    } 
    elseif ([string]::IsNullOrEmpty($str)) { 
     # Do not touch empty strings! 
    } 
    else { 
     # generate out string with matched prefix 
     $outString = "`"$prefix`",$str" 
     Write-Output $outString 
    } 
} 

$OutData | Out-File results.csv -Force -Encoding utf8 
+0

私は列に2行の質問を編集しました。 – Atermon

+0

@Atermon私はあなたがすでにそれを理解したのを見ている:) – n01d

0

foreach ($str in $text) { 

    if ($str -match '^Host:\s.*$') { 
     # get prefix 
     $prefix = $str -replace '^Host:\s?(.*)$','$1' 
    } 
    elseif ($str -match '^OS:\s.*$') { 
     # get prefix2 
     $prefix2 = $str -replace '^OS:\s?(.*)$','$1' 
    } 

    elseif ([string]::IsNullOrEmpty($str)) { 
     # Do not touch empty strings! 
    } 
    else { 
     # generate out string with matched prefix 
     $outString = "$prefix2`"$prefix`",$str" 
     Write-Output $outString 
    } 
} 
関連する問題