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

Linux Shell实现:替换含abuse@开头邮箱的行为指定格式行

Solution Using sed for Email Line Replacement

If you need to target lines containing emails starting with abuse@ and reformat them while leaving other lines untouched, sed is the go-to tool for this shell-based text manipulation.

Here's the command you can use:

sed -E 's/.*(abuse@[^[:space:]]+).*/email: \1/' input.txt > output.txt

Breakdown of the command:

  • -E: Enables extended regular expressions, so we don't have to escape parentheses for capturing groups.
  • .*(abuse@[^[:space:]]+).*: This regex matches the entire line, and captures the email starting with abuse@ (up to the first whitespace character—since emails don't typically include spaces, this ensures we grab the full valid email).
  • email: \1: Replaces the entire matched line with the formatted string, where \1 refers to the captured email address from the regex group.
  • input.txt: Your source file containing the original lines.
  • > output.txt: Writes the modified content to a new file (avoids overwriting your original data accidentally).

Example:

Original line:

aaaaaaaaaaaaaa abuse@xx.com

After running the command, this line becomes:

email: abuse@xx.com

Any lines without an abuse@ email will remain exactly as they are.

Bonus: In-place editing

If you want to modify the original file directly (use with caution!), you can use the -i flag. Note that the syntax varies slightly between GNU sed (most Linux systems) and BSD sed (macOS):

  • GNU sed:
    sed -i -E 's/.*(abuse@[^[:space:]]+).*/email: \1/' input.txt
    
  • BSD sed (macOS):
    sed -i '' -E 's/.*(abuse@[^[:space:]]+).*/email: \1/' input.txt
    

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.07 11:47:30