[英]How can I delete a line from a file in Powershell?
我在網上查了一下,發現了一個類似這樣的命令
Get-Content .\aws_hosts | Where-Object {$_ -notmatch '([^\.0-9])'} | Set-Content out.txt
所做的只是將該行復制到 out.txt 文件。 我想弄清楚如何從 .\aws_hosts 文件中刪除一行。
在我的 aws_hosts 文件中,我想專門刪除第 2 行的 IP 地址,而不是 [servers] 行
[servers]
35.35.35.25
這是我正在嘗試做的 linux 版本(它來自 Terraform 教程,他們使用的是 Linux,我只是不知道如何使用 Powershell 來完成)
sed -i '/^[0-9]/d' aws_hosts
只需使用與sed
相同的正則表達式:
Get-Content .\aws_hosts |
Where-Object { $_ -notmatch '^[0-9]' } |
Set-Content out.txt
對於較小的文件(大多數文本文件都是),您可以簡化為:
@(Get-Content .\aws_hosts) -notmatch '^[0-9]' | Set-Content out.txt
這種簡化的方法 - 由於預先完整讀取整個輸入文件 - 還允許您將結果寫回同一個文件,如sed -i
那樣。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.