gpt4 book ai didi

PowerShell - 删除txt文件中单词之间的空格

转载 作者:行者123 更新时间:2023-12-01 19:33:49 28 4
gpt4 key购买 nike

我是 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')
)

关于PowerShell - 删除txt文件中单词之间的空格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36934576/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com