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.

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.

Two small things before you go

If something didn’t work on your setup, leave a comment and tell me which distro and version you’re on. I read every one, and most of these articles got better because a reader pointed out what was missing.

And if it did work, share it. TecMint has stayed free for 14 years because people pass these articles around, and that traffic is what pays for writing the next one.

Leave a comment
Get two free Linux courses

Learn the Linux basics with two free courses and join 34,000+ readers getting practical TecMint tutorials and selected Pro TecMint articles every week.

Pro TecMint
Get lifetime access for $99

If you want to go beyond the free tutorials, Pro gives you 18 courses, 5 ebooks, and every future course I add. One payment, lifetime access, no renewals.

Ravi Saive
Ravi Saive
Administrator
About the author

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.

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.