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 withabuse@(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\1refers 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
相关产品推荐
相关产品推荐

