ThisCalculator

Everyday

Sed Number Replacement: How It Works

Updated · Standard formula, shown below

The calculator for this page is switched off while it is checked. The formula is below, and the worked example in the article shows how to use it by hand.

This article explains how to replace numbers using the sed stream editor, covering the exact syntax, common patterns, and practical examples. You will learn how to target digits, preserve decimals, handle multiple matches, and avoid the pitfalls that trip up most users.

Formula

The core sed substitution command follows this structure:

sed 's/pattern/replacement/flags'

  • s – the substitute command
  • pattern – a regular expression that matches the text you want to change
  • replacement – the text that replaces the matched portion
  • flags – optional modifiers like g (global), i (case-insensitive), or p (print)

For numbers specifically, the pattern is [0-9] (any single digit) or [0-9]+ (one or more digits). The + is a quantifier meaning “one or more of the preceding character or class.”

Worked Example

Using the default input abc123def456, let’s replace every digit with X:

  1. Start with the original text: abc123def456
  2. Apply the pattern [0-9]+ – it matches 123 and 456 as two separate groups.
  3. Replace each match with X.
  4. Result: abcXdefX

The command is sed 's/[0-9]+/X/g'. The g flag ensures all matches are replaced, not just the first on each line. Without g, only the first number in each line would change.

Why Use sed for Number Replacement?

sed is a lightweight, non-interactive text processor that excels at batch edits. It is available on virtually every Unix-like system (Linux, macOS, BSD) and is a standard tool in system administration and scripting. Unlike a full text editor, sed can process large files quickly and is easily embedded in pipelines or scripts.

Common use cases include:

  • Cleaning log files by masking IP addresses or IDs
  • Normalizing data – converting all numbers to a placeholder
  • Renumbering lists or code line references
  • Removing version numbers from filenames or config files

Basic Number Replacement Patterns

Here are the most frequently used patterns, each with a concrete example:

  • Replace first digit only: sed 's/[0-9]/X/' – changes abc123 to abcX23.
  • Replace all individual digits: sed 's/[0-9]/X/g' – changes abc123 to abcXXX.
  • Replace whole numbers (contiguous digits): sed 's/[0-9]\+/X/g' – changes abc123def456 to abcXdefX. Note the escaped + in basic regex.
  • Replace numbers with a specific length: sed 's/[0-9]\{3\}/X/g' – matches exactly three digits, so abc1234 becomes abcX4 (the first three digits are replaced, the fourth remains).
  • Replace numbers at the end of a line: sed 's/[0-9]\+$/X/' – changes abc123 to abcX.
  • Replace numbers at the beginning of a line: sed 's/^[0-9]\+/X/' – changes 123abc to Xabc.

Handling Decimals, Signs, and Scientific Notation

Numbers in real data often include more than just digits. A simple [0-9]+ will not match a decimal point or a minus sign. To handle these, you need to expand the pattern:

  • Decimal numbers: sed 's/[0-9]+\.[0-9]+/X/g' matches 3.14 but not 3 or .14 alone. For more flexibility, use sed 's/[0-9]*\.[0-9]\+/X/g' which also matches .5.
  • Negative numbers: sed 's/-[0-9]\+/X/g' matches -42. To include a possible sign, use sed 's/-\?[0-9]\+/X/g' – the \? makes the minus optional.
  • Scientific notation: sed 's/[0-9]\+\.[0-9]\+e[+-]?[0-9]\+/X/g' matches numbers like 1.23e-4. Note the ? must be escaped in basic regex.

Common Mistakes and How to Avoid Them

Even experienced users often fall into these traps:

  • Forgetting the g flag: Without it, only the first match per line is replaced. Always ask: do you want all numbers changed or just the first?
  • Using basic vs. extended regex: By default, sed uses basic regular expressions (BRE), where +, ?, and { must be escaped as \+, \?, \{. Using extended regex with -E (or -r in GNU sed) avoids the backslashes: sed -E 's/[0-9]+/X/g'.
  • Escaping the slash in replacement: If your replacement contains a slash (e.g., a path), use a different delimiter: sed 's|old|new|' or sed 's#old#new#'.
  • Matching numbers inside words: [0-9] matches any digit, even inside alphanumeric strings like abc123def. If you only want standalone numbers, use word boundaries: \b[0-9]\+\b (GNU sed supports \b).

Advanced Techniques and Alternatives

Sometimes you need more than a simple replace:

  • Capturing and reusing parts: Use \(...\) to capture and \1 to refer back. For example, to swap two numbers: sed 's/\([0-9]\+\)-\([0-9]\+\)/\2-\1/'.
  • Conditional replacement: Use the t command to loop until no more substitutions. For instance, to replace numbers that are multiples of 10 is not trivial with sed alone.
  • In-place editing: Use sed -i 's/[0-9]\+/X/g' file.txt to edit the file directly. On macOS, you need sed -i '' '...' file.txt (the empty string is required).
  • Alternatives: If your task involves complex logic, consider awk (better for column data), perl -pe 's/\d+/X/g' (more powerful regex), or a full scripting language like Python. For simple replacements, tr can replace single characters: tr '0-9' 'X' replaces each digit with X, but cannot handle multi-digit numbers as a whole.

Frequently Asked Questions

How do I replace all numbers in a file with sed?

Use sed -i 's/[0-9]\+/X/g' filename. The -i edits the file in place, and the g flag replaces every match on every line. Test without -i first to see the output.

Why does my sed command only replace the first number on each line?

Because you omitted the g flag. The g stands for “global” and tells sed to replace all matches on a line, not just the first. Add g at the end of the command: sed 's/[0-9]/X/g'.

How do I replace numbers but keep decimals like 3.14 intact?

Use a pattern that matches the whole decimal: sed 's/[0-9]\+\.[0-9]\+/X/g'. This matches digits, a dot, and more digits as one unit. If you want to also match integers, combine with an alternation: sed -E 's/[0-9]+\.[0-9]+|[0-9]+/X/g'.

What is the difference between sed and awk for number replacement?

sed is a stream editor that works line by line, ideal for simple substitutions. awk is a programming language that handles fields and columns, making it better when you need to replace numbers only in a specific column, or when you need arithmetic or conditional logic. For pure replace-all, sed is simpler and faster.

Can I replace numbers with a variable in sed?

Yes, but you must be careful with quoting. In a shell script, you can do: replacement="X"; sed "s/[0-9]\+/$replacement/g" file. Use double quotes around the sed script so the variable expands. If the variable contains special characters like & or \, escape them appropriately.

How do I replace only numbers that are exactly 5 digits long?

Use sed -E 's/\b[0-9]{5}\b/X/g'. The \b word boundaries ensure that the 5-digit number is not part of a longer number. The {5} quantifier matches exactly five digits. Without -E, you must escape the braces: sed 's/\b[0-9]\{5\}\b/X/g'.

Related

Formula used
in t | Text to process | - | abc123def456 | 0
in n | Number to replace | - | 999 | 0
in r | Replacement | - | X | 0
out out | Result | - | sed 's/[0-9]+/X/g' | 0

Each line is one input (in, sel, list) or one result (out: label, unit, formula, decimals). How the calculators work

Method: the standard formula for this calculation, rounded as each result states. See how the calculators work or report an error.