我正在尝试通过目录解析.txt文件(这部分是可以的)。 这些文件是报告,其中包含序列号列表,我也成功检索了该列表。 每个文件包含许多序列号和一个日期,我想构造一个数组,其中第一列将是序列号,第二列是重复日期,这是我失败的地方。 如何为所有序列号重复此日期并将其安排在CSV文件中?
要分析的文件示例:姓:[Calibrage-Vérification] [Sonde T Cryo] [17-01-2019 14h51mn24] .txt
Début de <<Calibrage / Vérification>>, <<Sonde T Cryo>> Le 17-01-2019 14h51mn24
******************************************************************************
** Conditions de mesures **
******************************************************************************
Nom de l'opérateur : AF
Condition de mesures :
Température : 21°C
Hygrométrie : 40%HR
Préssion atmosphérique : 980 HPA
Commentaire : NIM F85 / NIM F317 / NIM F100 / NOUVELLES SONDES NUMT 3M. 12199
Période de mesure = 15s
Nombre de point de mesure = 5
******************************************************************************
** Mesure Sonde en mode VERIFICATION (Moyenne) **
******************************************************************************
Mesure Brut Moyenne
Palier 1 (17-01-2019 23h51mn24)
Sonde 01 -18,263 -18,263 -18,263 -18,293 -18,263 -18,269
Sonde 02 -18,351 -18,361 -18,371 -18,341 -18,351 -18,355
Sonde 03 -18,336 -18,336 -18,326 -18,346 -18,336 -18,336
Sonde 05 -18,357 -18,357 -18,357 -18,357 -18,337 -18,353
******************************************************************************
** Contrôle d’anomalie de la mesure **
** Ecart max toléré entre la sonde et la Ref est de +-10°C **
******************************************************************************
Aucune anomalie constatée
******************************************************************************
** Les appareils ci-dessous sont Calibrés Et Vérifiés. **
******************************************************************************
Le seuil de tolerance est : +/- 0,2°C
Sonde n°01 [ AE190150013] Sonde n°02 [ AE190150014]
Sonde n°03 [ AE190150017] Sonde n°05 [ AE190150018]
Sonde n°06 [ AE190150015] Sonde n°07 [ AE190150020]
Sonde n°08 [ AE190150012] Sonde n°09 [ AE190150016]
Sonde n°10 [ AE190150011] Sonde n°11 [ AE190150019]
Sonde n°20 [ AE190150001] Sonde n°21 [ AE190150002]
******************************************************************************
** Les appareils ci-dessous n'ont pas passés le test de calibrage. **
******************************************************************************
Sonde n°04 [ ] Sonde n°22 [ ]
Sonde n°23 [ ] Sonde n°24 [ ]
Sonde n°25 [ ] Sonde n°26 [ ]
Sonde n°27 [ ] Sonde n°28 [ ]
Sonde n°29 [ ] Sonde n°30 [ ]
Sonde n°31 [ ] Sonde n°32 [ ]
Fin de <<Calibrage / Vérification>>, <<Sonde T Cryo>> Le 18-01-2019 04h52mn54
我想要得到的是: 第一行的日期 并且只有“ Les appareils ci-dessous sontCalibrésEtVérifiés”和“ Les appareils ci-dessous n'ont paspassésle calibrage”之间的序列号。
$data = 'C:\Users\user\Desktop\DEV\2019\2019_01 Janvier\*.txt'
[char[]]$replace = '!@#$%^&*(){}[]":;,<>/|\+=`~ '''
$regex = ($replace | % {[regex]::Escape($_)}) -join '|'
Get-ChildItem $data -recurse |
Where-Object { $_.Name -match $RegEx} |
Rename-Item -NewName {$_.Name -replace $RegEx, '_'}
(Get-ChildItem $data -recurse).FullName |
Foreach-Object {
(Get-Content $_ -Raw).
Replace('** Les appareils ci-dessous sont Calibrés Et Vérifiés. **','§').
Replace("Le seuil de tolerance est:+/-0,2°C",' ').
Replace("Leseuildetoleranceest:+/-0,2°C",'').
Replace("Le seuil de tolerance est:+/-0,3°C",' ').
Replace("Leseuildetoleranceest:+/-0,3°C",'').
Replace("** Les appareils ci-dessous n'ont pas passés le test de calibrage. **",'§').
Replace('*','').
Replace('n°','').
Replace('Sonde','').
Replace(' ','').
Replace(' ','') |
Set-Content $_
}
$folders = 'C:\Users\user\Desktop\DEV\2019\2019_01 Janvier'
$files = Get-ChildItem -Path $Folders -Include *.txt -Force -Recurse
foreach ($File in $Files) {
Foreach ($FileContent in $File) {
$FileContent = Get-Content -Path $File
$date = [regex]::matches($File,'\d{2}-\d{2}-\d{4}').value
}
$serial_temp = $FileContent -ireplace "^[^\[]*\[\s*","" -ireplace "\s*\][^\[]*\[\s*","`r`n" -ireplace "\]"
$serial = [regex]::Matches($serial_temp, '§([^/)]+)§') |ForEach-Object { $_.Groups[1].Value }
[System.String]$Text = $serial
$Keys = $serial -ireplace "^[^\[]*\[\s*","" -ireplace "\s*\][^\[]*\[\s*","`r`n" -ireplace "\]"
Write-Host $Serial $date
}
答案 0 :(得分:0)
现在您已经给我们提供了一个示例,可以为您提供帮助。
代码解析文件并导出包含四列的CSV文件:Serial
,Date
,Model
和第四列,以显示'Sonde'是通过还是未通过校准测试称为Status
。
(您可能会不理会那一个,但随后您可能会认为所有设备都可以正常工作。)
$folders = 'C:\Users\user\Desktop\DEV\2019\2019_01 Janvier'
$files = Get-ChildItem -Path $folders -Filter '*.txt' -Force -Recurse
$result = @()
foreach ($file in $files) {
# read the file as single string (-Raw) and split into 3 text blocks
$content = Get-Content -Path $file.FullName -Raw
# get the date from the file name
$date = [regex]::Matches($file.Name,'\d{2}-\d{2}-\d{4}').Value
if (!$date -and $content -match '\d{2}-\d{2}-\d{4}') {
# if date from file name failed, try and get it from the content
$date = $matches[0]
}
else {
# if still not date is found, take it from the file itself
$date = '{0:dd-MM-yyyy}' -f $file.LastWriteTime
}
# get the model number at the end of the 'Commentaire' line if any
$model = if ($content -match 'Commentaire\s*:.*\s(\d+)[\r?\n]') { $matches[1] } else { '' }
# split the content into text blocks and select the last two
$content = $content -split 'Les appareils ci-dessous' | Select-Object -Last 2
$re = [regex]'\[\s*(?<serial>\w+)'
foreach ($block in $content) {
if ($block -match 'Calibrés Et Vérifiés') {
$match = $re.Match($block)
while ($match.Success) {
$result += [PSCustomObject]@{
Serial = $match.Groups['serial'].Value
Date = $date
Model = $model
Status = 'Passed'
}
$match = $match.NextMatch()
}
}
elseif ($block -match "n'ont pas passés") {
$match = $re.Match($block)
while ($match.Success) {
$result += [PSCustomObject]@{
Serial = $match.Groups['serial'].Value
Date = $date
Model = $model
Status = 'Failed'
}
$match = $match.NextMatch()
}
}
}
}
$result | Export-Csv "C:\Users\user\DEV\somefile.csv" -Append -NoTypeInformation
使用示例文件,结果CSV文件将包含:
"Serial","Date","Model","Status" "AE190150013","17-01-2019","12199","Passed" "AE190150014","17-01-2019","12199","Passed" "AE190150017","17-01-2019","12199","Passed" "AE190150018","17-01-2019","12199","Passed" "AE190150015","17-01-2019","12199","Passed" "AE190150020","17-01-2019","12199","Passed" "AE190150012","17-01-2019","12199","Passed" "AE190150016","17-01-2019","12199","Passed" "AE190150011","17-01-2019","12199","Passed" "AE190150019","17-01-2019","12199","Passed" "AE190150001","17-01-2019","12199","Passed" "AE190150002","17-01-2019","12199","Passed"
希望能回答您的问题