powershell:落書き
$headers = "Header1", "Header2", "Header3" $arrayWithHeaders = @() # ヘッダーのみを持つオブジェクトを作成 $object = New-Object PSCustomObject foreach ($header in $headers) { $object | Add-Member -NotePropertyName $header -NotePropertyValue $null } # オブジェクトを配列に追加 $arrayWithHeaders += $object # 結果を表示 $arrayWithHeaders
# 既存のヘッダーの定義 $headers = "Header1", "Header2", "Header3" $arrayWithHeaders = @() # ヘッダーのみを持つオブジェクトを作成 $object = New-Object PSCustomObject foreach ($header in $headers) { $object | Add-Member -NotePropertyName $header -NotePropertyValue $null } # オブジェクトを配列に追加 $arrayWithHeaders += $object # 例としてのカンマ区切りの文字列配列 $commaSeparatedValues = "Value1,Value2,Value3", "ValueA,ValueB,ValueC" foreach ($line in $commaSeparatedValues) { $values = $line -split "," $object = New-Object PSCustomObject for ($i = 0; $i -lt $headers.Length; $i++) { $object | Add-Member -NotePropertyName $headers[$i] -NotePropertyValue $values[$i] } $arrayWithHeaders += $object } # 結果を表示 $arrayWithHeaders
# サンプルの配列 $array = " item1", "item2", " item3", "item4", " ", "item5" # 空白から始まる要素を除外 $filteredArray = $array | Where-Object { $_ -notmatch '^\s' } # 結果を表示 $filteredArray
$headers = "Header1", "Header2", "Header3" $arrayWithHeaders = @() # ヘッダーのみを持つオブジェクトを作成 $object = New-Object PSCustomObject foreach ($header in $headers) { $object | Add-Member -NotePropertyName $header -NotePropertyValue $null } $arrayWithHeaders += $object $commaSeparatedValues = "Value1,Value2,Value3", "unkoValueA,ValueB,ValueC", "ValueX,ValueY,ValueZ" foreach ($line in $commaSeparatedValues) { if (-not ($line -like "unko*")) { # 'unko' で始まらない場合の処理 $values = $line -split "," $object = New-Object PSCustomObject for ($i = 0; $i -lt $headers.Length; $i++) { $object | Add-Member -NotePropertyName $headers[$i] -NotePropertyValue $values[$i] } $arrayWithHeaders += $object } else { # 'unko' で始まる場合の処理 (必要に応じて変更してください) Write-Host "This line starts with 'unko': $line" } } $arrayWithHeaders
$file = "C:\path\to\your\largefile.log" $reader = [System.IO.File]::OpenText($file) $count = 0 while($reader.ReadLine() -ne $null) { $count++ } $reader.Close() Write-Output "Total lines: $count"
# 入力ファイルパス $inputPath = "C:\path\to\your\inputfile.txt" # 出力ファイルのベースパス $outputBasePath = "C:\path\to\output\outputfile" # ファイルを開いて読み込む $reader = [System.IO.StreamReader]::new($inputPath) # 変数の初期化 $lineCount = 0 $fileIndex = 1 $content = New-Object System.Collections.Generic.List[string] try { while ($null -ne ($line = $reader.ReadLine())) { # 行をリストに追加 $content.Add($line) $lineCount++ # 10000行ごとにファイルに書き出す if ($lineCount -eq 10000) { $outputPath = "$outputBasePath$fileIndex.txt" [System.IO.File]::WriteAllLines($outputPath, $content) # 変数をリセット $content.Clear() $lineCount = 0 $fileIndex++ } } # 最後のファイル(10000行未満の場合) if ($content.Count -gt 0) { $outputPath = "$outputBasePath$fileIndex.txt" [System.IO.File]::WriteAllLines($outputPath, $content) } } finally { $reader.Close() }
powershell/落書き.txt · 最終更新: 2024/05/13 06:50 by 133.106.206.103