Table of Contents#
- Understanding sed Fundamentals
- Static vs. Dynamic Replacement
- Method 1: Increment/Decrement Using Variables
- Method 2: Arithmetic Operations with External Commands
- Handling Multiple Numbers in a Line
- Real-World Use Cases
- Common Pitfalls and Troubleshooting
- Conclusion
- References
1. Understanding sed Fundamentals#
Core Syntax: Substitution
The s/pattern/replacement/flags command is sed's cornerstone:
sed 's/find/replace/' file.txt- Special characters:
^(start of line),$(end),.(any character),*(zero or more matches). - Capture groups: Use
-Efor extended regex, then use(...)to capture patterns and reference them with\1,\2.
Example: Simple Number Swap
Replace "42" with "99" globally:
sed 's/42/99/g' file.txt2. Static vs. Dynamic Replacement#
- Static: Direct value replacement (e.g.,
s/5/10/). - Dynamic: Modify numbers based on context or arithmetic (e.g., increment all numbers by 5).
- Challenge:
sedlacks built-in arithmetic. - Solution: Combine
sedwith variables, external commands, or scripting.
- Challenge:
3. Method 1: Increment/Decrement Using Variables#
Use shell variables for fixed arithmetic:
Example: Increment All Instances of "n" by 5
increment=5
sed -E "s/([0-9]+)/$((increment))\1/g" file.txtTranslation: Replace every number n with 5n.
Warning: This concatenates values instead of adding numerically (turns "10" into "510", not "15").
Workaround: Use expr or awk for true arithmetic (see Method 2).
4. Method 2: Arithmetic Operations with External Commands#
Offload calculations to tools like bc, expr, or awk.
Incrementing Numbers by 1#
sed -E 's/([0-9]+)/printf "%d\n" $((\1+1))/e' file.txt([0-9]+): Captures one or more digits using a capture group.printf "%d\n" $((\1+1)): Usesbasharithmetic to increment the captured number (\1).e:sedflag to execute the result.
Decrement by a Fixed Value (e.g., 3)#
sed -E 's/([0-9]+)/printf "%d\n" $((\1-3))/e' file.txtMultiply by a Constant#
sed -E 's/([0-9]+)/printf "%d\n" $((\1*2))/e' file.txtComplex Operations with bc#
Square all numbers:
sed -E 's/([0-9]+)/echo "\1 * \1" | bc/e' data.log5. Handling Multiple Numbers in a Line#
Process each number individually using the g flag and iteration:
Example: Increment Every Number in a Line
echo "ID: 42, Count: 7" | sed -E 's/([0-9]+)/printf "%d\n" $((\1+1))/e'
# Output: ID: 43, Count: 8Filter Specific Numbers
Only modify numbers following "Count:":
sed -E 's/Count: ([0-9]+)/printf "Count: %d" $((\1+1))/e' data.txt6. Real-World Use Cases#
-
Version Bumping in Files
Increment version numbers inversion.txt:sed -i -E 's/v([0-9]+)\./printf "v%d." $((\1+1))/e' version.txt -
Shift Timestamp Values
Add 100ms to all timestamps in a log:sed -E 's/(time: )([0-9]+)/\1printf "%d" $((\2+100))/e' app.log -
Code Refactoring
Shift array indices in Python scripts:sed -E 's/arr\[([0-9]+)\]/printf "arr[%d]" $((\1+5))/e' script.py
7. Common Pitfalls and Troubleshooting#
- Syntax Errors: Ensure capture groups
(...)and backreferences\1are properly escaped when not using-E. - Float Handling:
sedand$((...))only support integers. Usebc:sed -E 's/([0-9]+\.[0-9]+)/echo "\1 + 1.5" | bc/e' values.txt - Security: Avoid the
eflag with untrusted input (risk of code execution). - Performance: For large files, combine with
awk:awk '{gsub(/[0-9]+/, $0 + 100)}1' data.txt
8. Conclusion#
While sed isn’t designed for arithmetic, it becomes a dynamic number-manipulation powerhouse when combined with shell variables, execution flags, and external commands like bc. Use this guide as a reference for tasks from simple substitutions to complex incremental updates. For heavy numerical workflows, consider pairing sed with awk or Python scripts.
9. References#
sedManual: https://www.gnu.org/software/sed/manual/sed.html- Advanced Bash-Scripting Guide: https://tldp.org/LDP/abs/html/
bcCommand Documentation:man bc- POSIX Regular Expressions: https://www.regular-expressions.info/posix.html