You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何结合sed与grep修改匹配行字符并保留完整输出

解决仅替换含指定关键词行中字符的问题

不需要结合grep,单独用sed就能完成需求,一行命令即可:

sed '/two/s/X/-/g' test_hasham.txt

命令解释:

  • /two/:定位所有包含关键词“two”的行
  • s/X/-/g:仅在上述匹配到的行内,将所有“X”全局替换为“-”
  • 不匹配“two”的行会保持原样输出,完美满足保留所有行内容的要求

示例验证:

输入文件内容:

this is line one XXXXXX
this is line two XXXXXX
this is line three XXXXXX

执行命令后输出:

this is line one XXXXXX
this is line two ------
this is line three XXXXXX

内容的提问来源于stack exchange,提问作者Hasham1983

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.17 14:18:13