linux - 如何使用 sed 命令将 = 之后的值替换为 null
问题描述
我有一个场景,我想在不创建新文件的情况下替换digit or string
等于=
null [空白] 需要在同一个文件中替换
文件中的数据:
name=vilas
age=21
code=1345
需要替换后的code=1345
数字code=
我试过这个但卡住了
sed -i 's/^code=$/code=/1g' file.txt
注意: code= 之后的值将是动态的,需要使用我不擅长的正则表达式模式匹配
解决方案
根据您的问题:
在 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
推荐阅读
- azure-pipelines - Azure DevOps 2019 服务 - GitHub Azure Pipelines 应用程序 - 将 2 个构建管道关联中的 1 个删除到 GitHub 存储库
- html - 将文件路径插入大量文件
- python - 如何在支持多种数据格式的 Pandas 中合并日期?
- validation - 在 Azure AD 中创建用户之前,是否可以使用 Azure B2C 自定义策略验证来自社交身份提供商 (iDP) 的电子邮件声明?
- python - 我可以在python的for循环中更改for循环之外的变量吗?
- oracle - 查找当前月份和上个月值的总和
- python - 将全局变量传递给模块 - Python
- oracle - 警告:在 pl/sql 中使用编译错误创建的过程
- xcode - LLDB 问题 - 无法运行“po”
- c++ - C++ 中的奇怪结果