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
\(...\)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 "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 's/[0-9]\+/echo $((& + 1))/ge' file.txt[0-9]\+: Matches one or more digits.echo $((& + 1)): Usesbasharithmetic to increment the matched number (&).e:sedflag to execute the result.
Decrement by a Fixed Value (e.g., 3)#
sed 's/[0-9]\+/echo $((& - 3))/ge' file.txtMultiply by a Constant#
sed 's/[0-9]\+/echo $((& * 2))/ge' file.txtComplex Operations with bc#
Square all numbers:
sed 's/[0-9]\+/echo "& * &" | bc/ge' 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 's/[0-9]\+/echo $((& + 1))/ge'
# Output: ID: 43, Count: 8Filter Specific Numbers
Only modify numbers following "Count:":
sed 's/Count: \([0-9]\+\)/echo "Count: $((\1 + 1))"/ge' data.txt6. Real-World Use Cases#
-
Version Bumping in Files
Increment version numbers inversion.txt:sed -i 's/v[0-9]\+\./echo "v$((& + 1))."/ge' version.txt -
Shift Timestamp Values
Add 100ms to all timestamps in a log:sed 's/\(time: \)[0-9]\+/echo "\1$((& + 100))"/ge' app.log -
Code Refactoring
Shift array indices in Python scripts:sed 's/arr\[\([0-9]\+\)\]/echo "arr[$((\1 + 5))]"/ge' script.py
7. Common Pitfalls and Troubleshooting#
- Syntax Errors: Ensure capture groups
\(...\)and backreferences\1are properly escaped. - Float Handling:
sedand$((...))only support integers. Usebc:sed 's/\([0-9]\+\.[0-9]\+\)/echo "\1 + 1.5" | bc/ge' 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