shell 从JXML文件中删除PDF属性的脚本

fjnneemd  于 2023-05-01  发布在  Shell
关注(0)|答案(1)|浏览(108)

我有几个碧玉报告具有已弃用的属性(pdfFontNamepdfEncodingisPdfEmbedded),导致警告“PDF Font is deprecated and replaced by the Font Extension”。
由于有许多文件需要更正(可能超过一千个),我试图提出一个脚本,它可以自动从文件的源代码中删除这些属性及其值(用空字符串替换有问题的字符串)。
例如:
以下是某个文件源代码的代码行:

<font fontName="Courier New" size="10" isBold="false" isItalic="false" isUnderline="false" isStrikeThrough="false" pdfFontName="cour.ttf"/>

将成为:

<font fontName="Courier New" size="10" isBold="false" isItalic="false" isUnderline="false" isStrikeThrough="false" />

我已经尝试在Windows PowerShell上运行这个脚本:

Get-ChildItem "pathToFile" -Recurse | ForEach-Object {
(Get-Content $_.FullName).Replace('pdfFontName="\w+"','') | Set-Content $_.FullName
}

这个属性没有被删除,所以我尝试用实际的属性值替换正则表达式,如下所示:

Get-ChildItem "pathToFile" -Recurse | ForEach-Object {
(Get-Content $_.FullName).Replace('pdfFontName="Helvetica"','') | Set-Content $_.FullName
}

它起作用了。出于这个原因,我认为问题与正则表达式有关。我尝试了其他的正则表达式,但没有一个工作。你能帮我纠正一下吗?

mspsb9vt

mspsb9vt1#

正如圣地亚哥所指出的,对于基于regex的替换,使用-replace操作符而不是String类型的.Replace()方法(后者只执行 literal 替换):

(Get-Content $_.FullName) -replace 'pdfFontName="\w+"', ''

注意事项:

  • 如果替换操作数是空字符串(''),则可以 * 省略 *。
  • -replace-与.Replace()不同-默认情况下是大小写-* 不敏感 *;使用-creplace变体区分大小写。
  • -replace构建在[Regex]::Replace()(更强大)之上。NET方法;参见this answer了解它们的区别。

有关何时使用-replaceString.Replace(),参见this answer的底部。

相关问题