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

如何用单条sed命令移除单行冗余单词、指定关键词并添加邮箱

单条sed命令实现文本处理需求

需求说明

  • 移除单行内重复出现超过2次的冗余单词(同一单词最多保留2次)
  • 过滤文本中的TAGS、Owner、QA这三个关键词
  • 为每行末尾追加指定邮箱地址(示例:John@gmail.com)

示例输入

postgres    vp-postgres None    default:postgres-13 True    stopped db.m6g.large    13.4    vpc-95525af1
TAGS    Owner   John@gmail.com
TAGS        QA
TAGS    autostop    yes

示例输出

postgres    vp-postgres None    default:postgres-13 True    stopped db.m6g.large    13.4    vpc-95525af1 John@gmail.com
John@gmail.com John@gmail.com
 John@gmail.com
autostop    yes John@gmail.com

实现命令

sed -e ':a; s/\(\b\w\+\b\)\(\s\+\1\)\{2,\}/\1 \1/g; ta' -e 's/\b\(TAGS\|Owner\|QA\)\b\s*//g' -e 's/$/ John@gmail.com/' input.txt

命令拆解

  1. 处理重复单词:

    • :a 定义循环标签
    • s/\(\b\w\+\b\)\(\s\+\1\)\{2,\}/\1 \1/g 匹配同一单词连续重复3次及以上的情况,替换为仅保留2次
    • ta 如果替换发生,跳转到标签a继续循环处理,确保所有重复超过2次的单词都被处理
  2. 过滤指定关键词:

    • s/\b\(TAGS\|Owner\|QA\)\b\s*//g 匹配TAGS、Owner、QA整个单词,并删除单词及后面的多余空格
  3. 追加邮箱地址:

    • s/$/ John@gmail.com/ 在每行末尾添加指定邮箱地址

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.21 19:57:22