PowerShell - 从 txt 文件中删除单词之间的空格
PowerShell - Remove spaces between words from txt file
我是 powershell 的菜鸟,仍在尝试学习脚本,但在基础知识上磕磕绊绊。
我有一个包含网站列表的文本文件
Default Websites
Power Shell
Hello World
我需要删除单词之间的空格,以便得到以下结果
DefaultWebsites
PowerShell
HelloWorld
我试过使用 Trim('') 命令,但它没有删除空格。
这是我的原始脚本,它删除了开头和结尾的所有空格以及所有空白行。我需要在脚本中添加什么来删除单词之间的空格?
Get-WmiObject -Class IIsWebServerSetting -Namespace "root\microsoftiisv2" | Select ServerComment | Format-Table -HideTableHeaders | out-file c:\website.txt
$b = Get-Content -Path C:\website.txt
$b | ForEach {$_.TrimEnd()} | ? {$_.trim() -ne '' } > C:\website.txt
提前谢谢你。感谢您的帮助。
您可以使用带有简单正则表达式的 -replace
命令:
-replace '\s'
\s match any white space character [\r\n\t\f ]
试试这个:
[System.IO.File]::WriteAllText(
'C:\website.txt',
([System.IO.File]::ReadAllText('C:\website.txt') -replace '\s')
)
我会采用这种方法(英雄是 Replace 函数):
Get-WmiObject -Class IIsWebServerSetting -Namespace "root\microsoftiisv2" | Select ServerComment | Format-Table -HideTableHeaders | out-file c:\website.txt
$b = Get-Content -Path C:\website.txt
@(ForEach ($a in $b) {$a.Replace(' ', '')}) > C:\Website2.txt
我是 powershell 的菜鸟,仍在尝试学习脚本,但在基础知识上磕磕绊绊。 我有一个包含网站列表的文本文件
Default Websites
Power Shell
Hello World
我需要删除单词之间的空格,以便得到以下结果
DefaultWebsites
PowerShell
HelloWorld
我试过使用 Trim('') 命令,但它没有删除空格。
这是我的原始脚本,它删除了开头和结尾的所有空格以及所有空白行。我需要在脚本中添加什么来删除单词之间的空格?
Get-WmiObject -Class IIsWebServerSetting -Namespace "root\microsoftiisv2" | Select ServerComment | Format-Table -HideTableHeaders | out-file c:\website.txt
$b = Get-Content -Path C:\website.txt
$b | ForEach {$_.TrimEnd()} | ? {$_.trim() -ne '' } > C:\website.txt
提前谢谢你。感谢您的帮助。
您可以使用带有简单正则表达式的 -replace
命令:
-replace '\s'
\s match any white space character [\r\n\t\f ]
试试这个:
[System.IO.File]::WriteAllText(
'C:\website.txt',
([System.IO.File]::ReadAllText('C:\website.txt') -replace '\s')
)
我会采用这种方法(英雄是 Replace 函数):
Get-WmiObject -Class IIsWebServerSetting -Namespace "root\microsoftiisv2" | Select ServerComment | Format-Table -HideTableHeaders | out-file c:\website.txt
$b = Get-Content -Path C:\website.txt
@(ForEach ($a in $b) {$a.Replace(' ', '')}) > C:\Website2.txt