Text Processing (Grep/Sed/Awk)
Stop deleting lines by number and start matching patterns with sed
š§© The Challenge
You are trying to strip out a specific configuration block from a file, but the line numbers keep changing because someone else keeps editing the same damn config. Manually counting lines is a total waste of time and guarantees you will delete the wrong thing eventually.
š” The Fix
Use sed to match the range between two patterns instead of relying on line counts. It makes your cleanup scripts robust even when the file structure shifts around.
sed -i '/START_BLOCK/,/END_BLOCK/d' config.yaml
āļø Why It Works
Passing two regex patterns to sed tells it to delete everything from the first match to the second, effectively ignoring whatever line numbers they happen to be on. It is the only way to keep your sanity when managing dynamic config files.
š Pro-Tip: Use the -n flag and āpā command instead of ādā if you want to extract that block into a new file rather than just killing it.
Linux Tips & Tricks | Ā© ngelinux.com | 11/25/2026
