【问题标题】:Deleting Special character from a text file从文本文件中删除特殊字符
【发布时间】:2013-11-20 00:32:43
【问题描述】:

我有一个文本文件,我想从该文件中删除特殊字符。例如:- 文本文件中的条目

++Mathematics
--Chemistry
(Physics)
$#History)
%%Geography

现在我想替换文本文件中字符串中的所有特殊字符,并希望输出为:- 数学 化学 物理 历史 地理

我在我的 powershell 脚本中使用了替换命令,但尽管替换了特殊字符,它还是从我的文本文件中删除了所有值。

get-content D:\Share\SCCM-Powershell_Commands\edit.txt
Foreach-object {$_ -replace "\&", "" -replace "\£", "" -replace "\+", "" -replace "\-", "" -replace "\)","" -replace "\(","" -replace "\$","" -replace "\#","" -replace "\*",""}|
Set-Content D:\Share\SCCM-Powershell_Commands\edit1.txt

get-content D:\Share\SCCM-Powershell_Commands\edit.txt

同时有人可以建议如何开始学习 powershell。我是 powershell 的新手,想学习 Powershell 脚本。

【问题讨论】:

    标签: powershell-3.0


    【解决方案1】:

    试试这样的:

    foreach ($line in (Get-Content D:\Share\SCCM-Powershell_Commands\edit.txt)) {
        $line -replace "\&", "" -replace "\£", "" -replace "\+", "" -replace "\-", "" -replace "\)","" -replace "\(","" -replace "\$","" -replace "\#","" -replace "\*","" -replace "\%",""
    } | Set-Content D:\Share\SCCM-Powershell_Commands\edit1.txt
    
    Get-Content D:\Share\SCCM-Powershell_Commands\edit1.txt
    

    【讨论】:

      猜你喜欢
      • 2012-03-04
      • 1970-01-01
      • 1970-01-01
      • 2016-02-04
      • 2016-10-17
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多