Remove empty lines and lines containing only whitespace from notes.txt.
Leave lines with other content unchanged.
Which command is appropriate?
sed with the whitespace-only address and d command deletes matching pattern spaces before they are printed.
Detailed explanation
sed '/^[[:space:]]*$/p' notes.txtIncorrect. p prints matching lines in addition to the default output.
Incorrect. p prints matching lines in addition to the default output.
sed '/^$/d' notes.txtIncorrect. ^$ matches only truly empty lines, not whitespace-only lines.
Incorrect. ^$ matches only truly empty lines, not whitespace-only lines.
sed '/^[[:space:]]*$/d' notes.txtCorrect. The pattern covers empty and whitespace-only lines, and d deletes them.
Correct. The pattern covers empty and whitespace-only lines, and d deletes them.
grep -v '^$' notes.txtIncorrect. grep -v '^$' leaves whitespace-only lines.
Incorrect. grep -v '^$' leaves whitespace-only lines.
Try it yourself
An example you can run in a temporary verification environment.
printf 'alpha\n \n\nbeta\n' | sed '/^[[:space:]]*$/d'Expected result
alpha
betaKey points
- [[:space:]] matches whitespace
- * permits zero or more
- sed d deletes the current line
Notes
- Environment: GNU sed 4.x / C.UTF-8ロケール
- Command output formatting can vary slightly by distribution or tool version.
- Run the example in a temporary directory or process when possible.
Foundation review
Read the scope first
Check whether the command acts on the current shell, a new process, an existing process, or a file.
Verify the observable result
Use the supplied command and compare the output with the expected result.