如何使用 sed 命令将 = 后的值替换为 null
How to replace the value after = using sed command to null
我想将等于 =
之后的 digit or string
替换为 null [空白]
不创建新文件需要在同一个文件中替换
文件中的数据:
name=vilas
age=21
code=1345
需要将code=1345
后面的数字替换成code=
我试过这个但是卡住了
sed -i 's/^code=$/code=/1g' file.txt
注意:code= 之后的值将是动态的,需要使用我不擅长的正则表达式模式匹配
var1=456
$ sed -Ei "s/(code=).*/$var1/" file
name=vilas
age=21
code=456
根据您的问题:
在 MacOS 和 FreeBSD 上
sed -i '' -E 's/^code=[[:digit:]]+$/code=/g' test1.txt
在 CentOS 和 Debian 上
sed -i -E 's/^code=[[:digit:]]+$/code=/g' test1.txt
您可以更新角色 class 以满足您的需要。
哪里
-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
我想将等于 =
之后的 digit or string
替换为 null [空白]
不创建新文件需要在同一个文件中替换
文件中的数据:
name=vilas
age=21
code=1345
需要将code=1345
后面的数字替换成code=
我试过这个但是卡住了
sed -i 's/^code=$/code=/1g' file.txt
注意:code= 之后的值将是动态的,需要使用我不擅长的正则表达式模式匹配
var1=456
$ sed -Ei "s/(code=).*/$var1/" file
name=vilas
age=21
code=456
根据您的问题:
在 MacOS 和 FreeBSD 上
sed -i '' -E 's/^code=[[:digit:]]+$/code=/g' test1.txt
在 CentOS 和 Debian 上
sed -i -E 's/^code=[[:digit:]]+$/code=/g' test1.txt
您可以更新角色 class 以满足您的需要。
哪里
-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