Replacing numbers dynamically using sed

The sed command, a stream editor, is a highly powerful text processing tool available across all Linux distributions, including RHEL, CentOS, RockyLinux, and AlmaLinux. It is widely used for text manipulation tasks such as searching, finding and replacing text, and performing advanced scripting.

This guide will walk you through the process of using sed for dynamic number replacement, complete with examples tailored for enterprise Linux systems.


Understanding sed

The sed command processes text line by line and performs various transformations, including:

  • Searching for patterns.
  • Replacing text or numbers.
  • Deleting or inserting lines.
  • Modifying configuration files dynamically.

Basic syntax:

sed [options] 'command' file

Key components:

  • options: Additional flags like -i for in-place editing.
  • command: The operation to perform, such as substitution (s).
  • file: The file to process (optional if using standard input).

Common Use Cases for Dynamic Number Replacement

1. Basic Number Replacement

Replace a specific number in a file or input stream:

sed 's/old_number/new_number/' file

Example:

echo "Price: 100 dollars" | sed 's/100/200/'

Output:

Price: 200 dollars

2. Replacing All Numbers

Replace all occurrences of any number using regular expressions:

sed 's/[0-9]\+/replacement/g' file

Example:

echo "Items: 50, 100, 150" | sed 's/[0-9]\+/0/g'

Output:

Items: 0, 0, 0

3. Incrementing Numbers Dynamically

Increase numbers in text by a specific value.

Command:

sed -E 's/[0-9]+/echo $((\0 + increment))/ge'

Example:

echo "Item1: 100, Item2: 200" | sed -E 's/[0-9]+/echo $((\0 + 10))/ge'

Output:

Item1: 110, Item2: 210

4. Conditionally Replacing Numbers

Replace numbers based on a condition (e.g., greater than 50).

Command:

sed -E 's/[0-9]+/test \0 -gt threshold \&\& echo "High" || echo "Low"/e'

Example:

echo "Scores: 40, 80, 100" | sed -E 's/[0-9]+/test \0 -gt 50 \&\& echo "High" || echo "Low"/e'

Output:

Scores: Low, High, High

5. Updating Version Numbers

Automatically update version numbers in a configuration file.

File (config.txt):

AppVersion: 1.2.3
LibraryVersion: 4.5.6

Command:

sed -E 's/[0-9]+\.[0-9]+\.[0-9]+/2.0.0/' config.txt

Output:

AppVersion: 2.0.0
LibraryVersion: 2.0.0

6. Adjusting Prices with Percentage Increases

Increase numeric values (e.g., prices) by a percentage.

File (prices.txt):

Item1: 100
Item2: 200
Item3: 300

Command:

sed -E 's/[0-9]+/echo $((\0 + (\0 * 10 / 100)))/ge' prices.txt

Output:

Item1: 110
Item2: 220
Item3: 330

Using sed with In-Place Editing

To modify files directly, use the -i flag:

sed -i 's/old_number/new_number/' file

Advanced Tip: Combining sed with Other Tools

For complex manipulations, sed can be combined with tools like awk, grep, or bash for pre-processing data or applying additional logic.

Example: Replace numbers greater than 50 and append ” (Updated)”:

awk '{for(i=1;i<=NF;i++) if($i ~ /^[0-9]+$/ && $i > 50) $i=$i" (Updated)"; print}' file

Dynamic number replacement using sed is a critical skill for Linux system administrators and developers. Whether you’re editing configuration files, processing logs, or manipulating text, sed offers robust and efficient solutions for replacing numbers dynamically. Combined with other Linux utilities, it becomes even more powerful in handling diverse tasks.

Scroll to Top