Site icon New Generation Enterprise Linux

Stop deleting lines by number and start matching patterns with sed

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

0 0 votes
Article Rating
Exit mobile version