How to Use sed for Dynamic Number Replacement in Linux

The sed command, short for Stream Editor, is a powerful text processing tool in Linux, which is widely used for text manipulation tasks, including searching, finding and replacing text, and even performing advanced scripting.

This article will guide you through the basics of sed, explain how to use it for dynamic number replacement, and provide practical examples for beginners.

TecMint Weekly Newsletter
Get the Learn Linux 7 Days Crash Course free when you join 34,000+ Linux professionals reading every Thursday.
Check your email for a magic link to get started.
Something went wrong. Please try again.

What is sed?

The sed command processes text line by line, allowing you to:

  • Search for specific patterns.
  • Replace text or numbers.
  • Delete or insert lines.
  • Transform text in various ways.

It works non-interactively, meaning it can process files or streams of text without manual intervention.

Basic Syntax of sed Command

sed [options] 'command' file

Explanation:

  • options: Additional flags to modify sed’s behavior.
  • command: The operation to perform (e.g., substitution).
  • file: The file to process (optional if using standard input).

Replacing Numbers Dynamically with sed

Dynamic number replacement involves identifying numbers in text and replacing them based on specific conditions or patterns.

Here’s how you can achieve this with sed.

1. Basic Number Replacement

You can replace a specific number in a file using the substitution command s:

sed 's/old_number/new_number/' file

Explanation:

  • old_number: The number you want to replace.
  • new_number: The number to replace it with.

Example:

echo "The price is 100 dollars." | sed 's/100/200/'

The price is 200 dollars.

2. Replacing All Numbers

To replace all occurrences of any number, use a regular expression:

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

Explanation:

  • [0-9]\+: Matches one or more digits.
  • g: Replaces all matches in a line (global replacement).

Example:

echo "The items cost 100, 200, and 300 dollars." | sed 's/[0-9]\+/0/g'

The items cost 0, 0, and 0 dollars.

3. Incrementing Numbers Dynamically

Using sed, you can dynamically increment numbers by combining it with shell commands like awk or bash arithmetic.

echo "Item 1 costs 100, item 2 costs 200." | sed -E 's/[0-9]+/echo $((\0 + 10))/ge'

Item 1 costs 110, item 2 costs 210.

Explanation:

  • -E: Enables extended regular expressions.
  • \0: Refers to the matched number.
  • e: Executes the replacement as a command.

4. Replacing Numbers Based on Conditions

To replace numbers only if they match a condition (e.g., greater than a specific value), use a combination of sed and a scriptable command like awk.

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

Scores: Low, High, High

Explanation:

  • test \0 -gt 50: Checks if the number is greater than 50.
  • echo High || echo Low: Outputs “High” for numbers greater than 50 and “Low” otherwise.

5. Replace Version Numbers

You have a following configuration file (config.txt) containing version numbers, and you need to update them dynamically.

AppVersion: 1.2.3
LibraryVersion: 4.5.6

Dynamically updating version information in a configuration file.

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

Output:

AppVersion: 2.0.0
LibraryVersion: 2.0.0

6. Add a Percentage to Numbers

In this example, you might have a file (prices.txt) containing prices of various items, and you want to increase all the prices by a specific percentage, such as 10%.

Item1: 100
Item2: 200
Item3: 300

In the above file, you have a list of items along with their respective prices and you want to increase each price by 10%, use:

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

Output:

Item1: 110
Item2: 220
Item3: 330
Conclusion

Dynamic number replacement with sed in Linux is a versatile skill for any Linux user or system administrator. By understanding sed’s basic syntax and combining it with regular expressions and shell commands, you can handle various text manipulation tasks efficiently.

If this article helped, with someone on your team.

TecMint Weekly Newsletter
Get the Learn Linux 7 Days Crash Course free when you join 34,000+ Linux professionals reading every Thursday.
Check your email for a magic link to get started.
Something went wrong. Please try again.
TecMint has been free for 14 years. Help keep it that way.
Google AI Overviews and tools like ChatGPT have cut into search traffic for independent tech sites like TecMint. Running this site costs over $2,000 every month for hosting, infrastructure, and paying authors to keep the content accurate and tested.

If this article helped you solve a problem, consider buying a coffee. It helps keep TecMint free, supports the authors, and keeps the project going.
☕ Buy Me a Coffee
Ravi Saive
I'm Ravi Saive, an award-winning entrepreneur and founder of several successful 5-figure online businesses, including TecMint.com, GeeksMint.com, UbuntuMint.com, and the premium learning hub Pro.Tecmint.com.

Each tutorial at TecMint is created by a team of experienced Linux system administrators so that it meets our high-quality standards.

Got Something to Say? Join the Discussion...

Thank you for taking the time to share your thoughts with us. We appreciate your decision to leave a comment and value your contribution to the discussion. It's important to note that we moderate all comments in accordance with our comment policy to ensure a respectful and constructive conversation.

Rest assured that your email address will remain private and will not be published or shared with anyone. We prioritize the privacy and security of our users.

Free Course
Get a free Linux course before you go.
Subscribe to TecMint Weekly and get the Learn Linux 7 Days Crash Course free. Read by 34,000+ Linux professionals every Thursday.
Something went wrong. Please try again.
Check your email for a magic link to get started.