我有一個場景,我想將digit or stringafter替換=為 null [ blank ] 而不創建新檔案需要在同一個檔案中替換
檔案中的資料:
name=vilas
age=21
code=1345
后需要更換數字code=1345來code=
我試過這個但卡住了
sed -i 's/^code=$/code=/1g' file.txt
注意:code= 之后的值將是動態的,需要使用我不擅長的正則運算式模式匹配
uj5u.com熱心網友回復:
var1=456
$ sed -Ei "s/(code=).*/\1$var1/" file
name=vilas
age=21
code=456
uj5u.com熱心網友回復:
根據您的問題:
在 MacOS 和 FreeBSD 上
sed -i '' -E 's/^code=[[:digit:]] $/code=/g' test1.txt
在 CentOS 和 Debian 上
sed -i -E 's/^code=[[:digit:]] $/code=/g' test1.txt
您可以更新字符類以滿足您的需要。
在哪里
-i Edit files in-place
-E Interpret regular expressions as extended (modern) regular expressions rather than basic regular expressions (BRE's).
s The substitute command
g Apply the replacement to all matches to the regexp, not just the first.
GNU 檔案:https : //www.gnu.org/software/sed/manual/html_node/Character-Classes-and-Bracket-Expressions.html SED 檔案:https : //www.gnu.org/software/sed/manual /sed.html
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/317387.html
上一篇:C-Execv-不兼容的指標型別
