這是我在 PowerShell 5.1 中使用的:
(gc test.txt) | ForEach-Object {$_ -replace '^(.*)(\r?\n|$)', '^(.*)(\r?\n|$)', 'this is a repl $1\ntest tex $1 test$2'} | Set-Content test2.txt
這是test.txt:
ok
ok
ok
這是當前的test2.txt:
this is a repl ok\ntest tex ok test
雖然它應該是:
first ok
second ok third
我也試過這個但得到以下錯誤:
sed -re "s|^(.*)(\r?\n|$)|this is a repl $1\ntest tex $1 test$2" test.txt > test2.txt
錯誤:
/usr/bin/sed: -e expression #1, char 19: unknown option to `s'
我也試過這個(這在 Linux 中有效,但在 Windows 中無效):
sed -e "s|^(.*)(\r?\n|$)|this is a repl $1\ntest tex $1 test$2" -i test.txt
/usr/bin/sed: -e expression #1, char 19: unknown option to `s'
如何查找和替換正則運算式?
我搜索了 google 和 stackoverflow 并嘗試了一些命令,但它們\n在我的問題中顯示如上。
uj5u.com熱心網友回復:
在 PowerShell 中的單引號內,不支持字串轉義序列。'\n是一個文字序列,\后跟一個文字n字符。
您需要使用雙引號,然后使用反引號 n插入換行符,即"`n". 所以,使用
"this is a repl `$1`ntest tex `$1 test`$2"
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/487124.html
