如何使用sed命令将单个数字替换为对应英文读法,保留多位数?
Got it, let's tackle this problem. The core requirement is to replace every standalone single digit (0-9) with its English word equivalent, while leaving multi-digit numbers untouched. We also need to handle digits at the start or end of a line correctly.
The Sed Command
Here's the complete command that does exactly what you need:
sed -E -e 's/(^|[^0-9])0([^0-9]|$)/\1zero\2/g' \ -e 's/(^|[^0-9])1([^0-9]|$)/\1one\2/g' \ -e 's/(^|[^0-9])2([^0-9]|$)/\1two\2/g' \ -e 's/(^|[^0-9])3([^0-9]|$)/\1three\2/g' \ -e 's/(^|[^0-9])4([^0-9]|$)/\1four\2/g' \ -e 's/(^|[^0-9])5([^0-9]|$)/\1five\2/g' \ -e 's/(^|[^0-9])6([^0-9]|$)/\1six\2/g' \ -e 's/(^|[^0-9])7([^0-9]|$)/\1seven\2/g' \ -e 's/(^|[^0-9])8([^0-9]|$)/\1eight\2/g' \ -e 's/(^|[^0-9])9([^0-9]|$)/\1nine\2/g' input.txt
How It Works
Let's break down the logic behind each replacement rule:
-E: Enables extended regular expressions, so we don't have to escape parentheses (()) which makes the pattern cleaner.- The regex pattern
(^|[^0-9])matches either the start of a line (^) or a non-digit character ([^0-9]). This ensures we don't match the first digit of a multi-number (like the "2" in "23"). - The middle part
([0-9])targets the single digit we want to replace. - The final part
([^0-9]|$)matches either a non-digit character or the end of a line ($), ensuring we don't match the last digit of a multi-number (like the "3" in "23"). \1and\3preserve the characters before and after the digit (if any), so we don't mess up the surrounding text. We just swap the digit in\2with its English equivalent.
Testing with Your Example
If you run the command on your input string:
5 word 23 word 1 word
You'll get the expected output:
five word 23 word one word
Edge Cases Handled
This command works for all edge scenarios you might encounter:
- Line-start digits:
3 hello→three hello - Line-end digits:
hello 7→hello seven - Isolated single-digit lines:
9→nine - Digits adjacent to non-alphanumeric characters:
!4?→!four?
Note: If you're using BSD sed (common on macOS), replace -E with -r—the rest of the command stays the same.
内容的提问来源于stack exchange,提问作者Hilbert Erwin




