因此,我有2个文件具有相同的所列内容样式-字体ID,字体定义和时间戳。我想获取第二个新字体文件,并替换第一个文件中具有匹配字体ID的行-使用powershell(无需数据库,这将极大地简化)。
File2文本行= [FontIDA01] 5,5,5,5,randomtext,11/10/2001应该替换File1中[FontIDA01]匹配的行,并将5,5,5,5替换为6,6, 6,6,以及日期以及该行上的日期。
$content = Get-Content $fileSelected #(path chosen by user)
$masterContent = Get-Content $masterContentPath #(hardcoded path)
foreach($line in content)
{
$fontID = $line.SubString($startFontID, $endFontID)#this just sets font id = 23jkK instead of [23jkK]
foreach($masterLine in $masterContent)
{
if ($masterLine.Contains($fontID))
{
$masterContent -replace $masterLine, $line where-Object{$_.Name -contains $fontID} | Set-Content $masterContent -raw
}
}
}
我什至靠近吗?
收集字典中的新数据并将其用于替换:
# get new data in a dictionary
$newData = @{}
Get-Content 2.txt | %{
$parts = $_ -split ' '
$newData[$parts[0]] = @{numbers=$parts[1]; date=$parts[3]}
}
#patch original data using the new data dictionary
Get-Content 1.txt | %{
$parts = $_ -split ' '
$id = $parts[0]
$new = $newData[$id]
if ($new) {
$id, $new.numbers, $parts[2], $new.date -join ' '
} else {
$_
}
} | Out-File 3.txt -Encoding utf8
该代码假设字段之间用空格隔开,因此,如果不是这种情况,则必须使用其他方法来提取部分内容,例如Select-String
regexp或regexp if ($_ -match '(.+?) ([\d,]+) and so on') { $id = $matches[0] }
。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句