Sed Stream Edit In Place Backup
Perform in place stream editing with sed using a backup suffix and multiple expression commands for safe text transformation.
Code
#!/usr/bin/env bash
set -euo pipefail
FILE="config.txt"
sed -i.bak \
-e 's/DEBUG = false/DEBUG = true/' \
-e 's/PORT = .*/PORT = 8080/' \
-e '/^$/d' \
"${FILE}"
echo "Edited ${FILE}, backup at ${FILE}.bak"How it works
set -euo pipefail removes the shell’s usual optimism — errors abort, empty variables abort, hidden pipeline failures abort.
The work is delegated to sed, chained so each stage’s output feeds the next.
Interactive Tools for This Topic
Git Conventional Commit BuilderBuild and validate Conventional Commits v1.0.0 messages with type, scope, breaking markers, 72-char subject counter, body, footers, and git CLI command.Glob Pattern TesterCompile glob patterns with *, **, ?, character classes, and braces into regex and test them against a list of candidate file paths.Levenshtein Distance CalculatorCompute the classic edit distance between two strings with a space-optimized DP and a normalized similarity percentage.Tar Archive Command BuilderBuild safe tar create, extract, and list commands for gzip, xz, zstd, and bzip2 archives with excludes, target directory (-C), and flag explanations.
FAQ
- Does the script stop on the first error?
- Yes — `set -euo pipefail` aborts on the first failing command, treats unset variables as errors, and fails the whole pipeline if any stage fails. Nothing continues half-broken.
- Which external commands does the script rely on?
- `sed` must be available on the PATH environment variable; everything else is shell built-in syntax.
Report a problem with this exampleOpens a pre-filled issue on GitHub.