当前位置:网站首页 > 更多 > 玩电脑 > 正文

[玩转系统] PowerShell 替换文件中的行

作者:精品下载站 日期:2024-12-14 16:50:21 浏览:13 分类:玩电脑

PowerShell 替换文件中的行


使用PowerShell Replace()方法或PowerShell replace运算符,它可以轻松替换文件中的一行。您可以使用 Set-Content cmdlet 替换文件中的字符串并将更新的内容保存到文件中。

要在 PowerShell 中读取文件,我们将使用 Get-Content cmdlet,并在替换文件中的行后,使用 Set-Content 将内容保存在文件中cmdlet。

在本文中,我们将讨论如何使用 PowerShell Replace() 方法或替换运算符替换文件中的行。

PowerShell 使用 Replace() 方法替换文件中的行

PowerShell 具有 Get-Content cmdlet 来读取文件内容。使用PowerShell String内置方法replace(),我们可以轻松替换文件中的一行或替换文件中的文本。

# Read the file content using the Get-Content
$filecontent = Get-Content -Path D:\PS\PS-String.txt -Raw

#Print the file content
$filecontent

# Replace a line in a file

$str.Replace("Refer the link https://shellgeek.com/tag/active-directory-2/","Refer the link Https://ShellGeek.com/")

# Save the replace line in a file  

$filecontent.Replace("Refer the link https://shellgeek.com/tag/active-directory-2/","Refer the link Https://ShellGeek.com/") | Set-Content -Path D:\PS\PS-String.txt                  

    

在上面的 PowerShell 脚本中,Get-Content cmdlet 使用 Path 参数指定要读取的文本文件,并使用 Raw 参数存储整个文件内容作为变量 $filecontent 中的字符串。

要替换文件中的行,请使用带有两个参数的 PowerShell Replace() 方法;要查找的字符串和要替换为找到的文本的字符串。

替换文件中的行后,将输出通过管道传输到 Set-Content cmdlet 以保存文件。

上述用于替换文件中的行的 PowerShell 脚本的输出是:

[玩转系统] PowerShell 替换文件中的行

酷提示:如何在 PowerShell 中替换字符串中的特殊字符!

使用 PowerShell 替换运算符替换文件中的行

使用替换运算符,我们可以替换文件中的行。使用 Get-Content 文件读取文件内容并将其存储在变量 $filecontent 中。

$filecontent 上使用 PowerShell 替换运算符来替换要搜索的行以及要在其参数中替换的行。

# Read the file content using Get-Content
$filecontent = Get-Content -Path D:\PS\PS-String.txt -Raw

$filecontent

# Use replace operator to replace a line in file
$filecontent -replace 'Refer the link https://shellgeek.com/tag/active-directory-2/','Refer the link 
 Https://ShellGeek.com' 

在上面的 PowerShell 脚本中,replace 运算符将行“Refer the link https://shellgeek.com/tag/active-directory-2/”替换为“ >参考链接https://ShellGeek.com

替换文件中的一行后,上述 PowerShell 脚本的输出为:

PS C:\> $filecontent = Get-Content -Path D:\PS\PS-String.txt -Raw                                                       
PS C:\>                                                                                                                 

PS C:\> $filecontent
Hello SysAdmin, Welcome to PowerShell Programming.
Let's begin your Active Directory management using the PowerShell journey here.

Refer the link https://shellgeek.com/tag/active-directory-2/

PS C:\>                                                                                                                 

PS C:\> $filecontent -replace 'Refer the link https://shellgeek.com/tag/active-directory-2/','Refer the link Https://ShellGeek.com'                                                                                                             

Hello SysAdmin, Welcome to PowerShell Programming.
Let's begin your Active Directory management using the PowerShell journey here.

Refer the link Https://ShellGeek.com

PS C:\>                                                                                                                                                                                                                                                              

酷提示:如何在 PowerShell 中用通配符替换所有文本!

结论

我希望上面关于如何使用 PowerShell Replace() 方法或 PowerShell Replace 运算符替换文件中的行的文章对您有所帮助。

您可以在 ShellGeek 主页上找到有关 PowerShell Active Directory 命令和 PowerShell 基础知识的更多主题。

您需要 登录账户 后才能发表评论

取消回复欢迎 发表评论:

关灯