From Pieces to a One-Liner
You now have the building blocks: an address selects lines, the s command replaces text, and options like -n and -i control output. A one-liner simply assembles these into a single command that does something worthwhile. The classic first example is cleaning a config file: strip comments, drop blank lines, and normalise whitespace. We will build it up piece by piece so every character earns its place.
Cricket analogy: Assembling a one-liner is like combining a chosen field, a specific delivery, and a game plan into one over — each element you learned separately now works together for a single wicket.
Building It Step by Step
Start by deleting full-line comments with the address /^#/d, which removes every line whose first character is #. Chain a second command to drop empty lines with /^$/d. Finally add a substitution to strip trailing whitespace, s/[ \t]*$//, which matches any run of spaces or tabs at end of line and replaces it with nothing. Joined by semicolons, these three commands become one readable pipeline that sanitises a file in a single pass.
Cricket analogy: Building step by step is like setting your field one fielder at a time — slip first, then gully, then point — until the whole ring is arranged for the delivery.
# Clean a config file: drop comments, blanks, and trailing whitespace
sed '/^#/d; /^$/d; s/[ \t]*$//' app.conf
# Preview the change safely (writes to stdout, file untouched)
sed '/^#/d; /^$/d; s/[ \t]*$//' app.conf | lessOrder matters within a one-liner: sed applies commands top to bottom on each line. Deleting a line with 'd' immediately ends processing for that line and starts the next cycle, so any commands after a 'd' never run on a deleted line.
Making It Permanent
Once the preview looks right, apply the change in place with -i. Always run the command first without -i so you can inspect the output; only when satisfied add the flag. Using -i.bak keeps a safety copy of the original, which is invaluable if the regex was subtly wrong. This test-then-commit habit is the single best defence against a one-liner that quietly corrupts a file across an entire directory.
Cricket analogy: Testing before -i is like a batsman shadow-practising a shot in the nets before playing it in a Test match, where a mistake cannot be taken back.
# Apply in place, keeping a .bak backup of the original
sed -i.bak '/^#/d; /^$/d; s/[ \t]*$//' app.conf
# If it went wrong, restore from the backup
mv app.conf.bak app.confNever run 'sed -i' across many files (e.g. with a glob or find) until you have verified the command on a single sample file. An incorrect regex applied in place has no undo, and without a backup suffix the original content is gone.
- A one-liner combines addresses, the s command, and options into one command.
- Chain commands with semicolons; they run top to bottom on each line.
- A 'd' command ends the cycle for that line, so later commands are skipped.
- Preview on stdout first, then commit with -i once the output is correct.
- Use -i.bak to keep a backup of the original before in-place editing.
- Test on one file before applying -i across many — there is no undo.
Practice what you learned
1. In a chained script, what happens after a 'd' command deletes a line?
2. What does the address/command '/^$/d' do?
3. Why run a sed one-liner without -i first?
4. What does 'sed -i.bak' provide that 'sed -i' does not?
5. What does 's/[ \t]*$//' accomplish?
Was this page helpful?
You May Also Like
What Is sed?
An introduction to sed, the non-interactive stream editor that transforms text line by line as it flows through a pipeline.
sed Substitution Basics
A practical guide to the s command — sed's find-and-replace engine — covering delimiters, flags, backreferences, and the ampersand.
Addresses and Line Selection
How to target specific lines in sed using line numbers, regex patterns, ranges, and the negation operator so commands run only where you want.
Running sed Scripts
How to invoke sed from the command line and from script files, including the key options that control input, output, and multi-command execution.
Related Reading
Related Study Notes in Programming
Browse all study notesApache Spark Study Notes
Programming · 30 topics
ProgrammingApache Flink Study Notes
Programming · 30 topics
ProgrammingHadoop Study Notes
Programming · 30 topics
ProgrammingSnowflake Study Notes
Programming · 30 topics
ProgrammingApache Airflow Study Notes
Programming · 30 topics
Programmingdbt (Data Build Tool) Study Notes
Programming · 30 topics