Add a prefix only to non-empty lines.

Section: sed and Stream Editing

Prefix non-empty lines

bash
bash
sed -E '/^$/! s/^/# /' notes.txt
Explanation

Combines a regex address with substitution.

Learn the surrounding workflow

Compare similar commands or jump into common fixes when this command is part of a bigger troubleshooting path.

Related commands

Same sheet · prioritizing sed and Stream Editing
Reformat date
Convert YYYY-MM-DD into MM/DD/YYYY.
OpenIn sheetbashsame section
Trim trailing spaces
Remove trailing whitespace from each line.
OpenIn sheetbashsame section
Extract quoted content
Print only the content inside double quotes on each line.
OpenIn sheetbashsame section
sed regex replacement
Replace all runs of whitespace with a single space.
OpenIn sheetbash1 tag match
grep whole word
Find full-word matches in files.
Atomic group in PCRE
Avoid backtracking inside a grouped token.