100% Free Forever
AI-Powered Learning
Industry Expert Content
Certificates & Badges
Learn At Your Own Pace
Programming

Your First sed One-Liner

Build a genuinely useful sed one-liner from scratch, combining an address, a substitution, and options into a single practical command.

FoundationsBeginner8 min readJul 10, 2026
Analogies

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.

bash
# 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 | less

Order 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.

bash
# 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.conf

Never 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

Was this page helpful?

Topics covered

#Programming#SedStreamEditorStudyNotes#YourFirstSedOneLiner#Sed#One#Liner#Pieces#StudyNotes#SkillVeris#ExamPrep