我有以下代碼來匹配模式并將其保存在 CSV 檔案中。我需要將 regex1 和 regex2 作為 col1 和 col2 保存在 csv 中,而不是全部保存在第一個 col 中。
$inputfile = ( Get-Content D:\Users\naham1224\Desktop\jil.txt )
$FilePath = "$env:USERPROFILE\Desktop\jil2.csv"
$regex1 = "(insert_job: [A-Za-z]*_*\S*)"
$regex2 = "(machine: [A-Z]*\S*)"
$inputfile |
Select-String -Pattern $regex2,$regex1 -AllMatches |
ForEach-Object {$_.matches.groups[1].value} |
Add-Content $FilePath`
輸入檔案包含:input.txt
/* ----------------- AUTOSYS_DBMAINT ----------------- */
insert_job: AUTOSYS_DBMAINT job_type: CMD
command: %AUTOSYS%\bin\DBMaint.bat
machine: PWISTASASYS01
owner: svc.autosys@cbs
permission:
date_conditions: 1
days_of_week: su,mo,tu,we,th,fr,sa
start_times: "03:30"
description: "Runs DBmaint process on AE Database - if fails - MTS - will run next scheduled time"
std_out_file: ">$$LOGS\dbmaint.txt"
std_err_file: ">$$LOGS\dbmaint.txt"
alarm_if_fail: 0
alarm_if_terminated: 0
send_notification: 0
notification_msg: "Check DBMaint output in autouser.PD1\\out directory"
notification_emailaddress: [email protected]
/* ----------------- TEST_ENV ----------------- */
insert_job: TEST_ENV job_type: CMD
command: set
machine: PWISTASASYS01
owner: svc.autosys@cbs
permission:
date_conditions: 1
days_of_week: su,mo,tu,we,th,fr,sa
start_times: "03:30"
description: "output env"
std_out_file: ">C:\Users\svc.autosys\Documents\env.txt"
std_err_file: ">C:\Users\svc.autosys\Documents\env.txt"
alarm_if_fail: 1
alarm_if_terminated: 1
電流輸出:
電流輸出
預期輸出:
預期產出
我正在嘗試各種方法來做到這一點,但沒有運氣。非常感謝任何建議和幫助。
uj5u.com熱心網友回復:
這是我將如何做到這一點:
$inputPath = 'input.txt'
$outputPath = 'output.csv'
# RegEx patterns to extract data.
$patterns = @(
'(insert_job): ([A-Za-z]*_*\S*)'
'(machine): ([A-Z]*\S*)'
)
# Create an ordered Hashtable to collect columns for one row.
$row = [ordered] @{}
# Loop over all occurences of the patterns in input file
Select-String -Path $inputPath -Pattern $patterns -AllMatches | ForEach-Object {
# Extract key and value from current match
$key = $_.matches.Groups[ 1 ].Value
$value = $_.matches.Value
# Save one column of current row.
$row[ $key ] = $value
# If we have all columns of current row, output it as PSCustomObject.
if( $row.Count -eq $patterns.Count ) {
# Convert hashtable to PSCustomObject and output (implicitly)
[PSCustomObject] $row
# Clear Hashtable in preparation for next row.
$row.Clear()
}
} | Export-Csv $outputPath -NoTypeInformation
輸出 CSV:
"insert_job","machine"
"insert_job: AUTOSYS_DBMAINT","machine: PWISTASASYS01"
"insert_job: TEST_ENV","machine: PWISTASASYS01"
評論:
使用
Select-Stringwith 引數-Path我們不必事先讀取輸入檔案。有序哈希表(字典)用于收集所有列,直到我們輸出一整行。這是生成多列而不是在單個列中輸出所有資料的關鍵步驟。
將 Hashtable 轉換為 a
PSCustomObject是必要的,因為Export-Csv需要物件,而不是字典。雖然 CSV 看起來像您的“預期輸出”并且您可能有充分的理由期待它,但在 CSV 檔案中,值通常不應重復列名。要從值中洗掉列名,只需替換
$value = $_.matches.Value為$_.matches.Groups[ 2 ].Value,這會產生如下輸出:"insert_job","machine" "AUTOSYS_DBMAINT","PWISTASASYS01" "TEST_ENV","PWISTASASYS01"
至于你嘗試過的:
Add-Content僅從字串輸入寫入純文本檔案。雖然您可以使用它來創建 CSV 檔案,但您必須自己添加分隔符和轉義字串,這很容易出錯并且比必要的麻煩更多。Export-CSVotoh 將物件作為輸入,并自動關心所有 CSV 格式的詳細資訊。
uj5u.com熱心網友回復:
正如 zett42 提到Add-Content的那樣,這并不是最適合的。由于您正在尋找由逗號分隔的多個值,Export-Csv因此您可以使用它。 Export-Csv將從管道中獲取物件,將它們轉換為逗號分隔的屬性行,添加標題行并保存到檔案
我的解決方案在這里采用了一些不同的方法。我已經將不同的正則運算式模式組合成一個,這將為我們提供一個包含作業和機器名稱的匹配項。
$outputPath = "$PSScriptRoot\output.csv"
# one regex to match both job and machine in separate matching groups
$regex = '(?s)insert_job: (\w ). ?machine: (\w )'
# Filter for input files
$inputfiles = Get-ChildItem -Path $PSScriptRoot -Filter input*.txt
# Loop through each file
$inputfiles |
ForEach-Object {
$path = $_.FullName
Get-Content -Raw -Path $path | Select-String -Pattern $regex -AllMatches |
ForEach-Object {
# Loop through each match found in the file.
# Should be 2, one for AUTOSYS_DBMAINT and another for TEST_ENV
$_.Matches | ForEach-Object {
# Create objects with the values we want that we can output to csv file
[PSCustomObject]@{
# remove next line if not needed in output
InputFile = $path
Job = $_.Groups[1].Value # 1st matching group contains job name
Machine = $_.Groups[2].Value # 2nd matching group contains machine name
}
}
}
} | Export-Csv $outputPath # Pipe our objects to Export-Csv
output.csv 的內容
"InputFile","Job","Machine"
"C:\temp\powershell\input1.txt","AUTOSYS_DBMAINT","PWISTASASYS01"
"C:\temp\powershell\input1.txt","TEST_ENV","PWISTATEST2"
"C:\temp\powershell\input2.txt","AUTOSYS_DBMAINT","PWISTASAPROD1"
"C:\temp\powershell\input2.txt","TEST_ENV","PWISTATTEST1"
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/490717.html
標籤:电源外壳
