The current solution (2025-03-26)
sed -i -r \
-e '/^[[:space:]]*[#;]/d' \
-e '/^[[:space:]]*$/d' \
<file> &&
sed -i -z 's/\n$//' <file>
How to remove the trailing newline (LF
) character (the terminal newline, the EOF newline) from a text file using sed
?
The previous solution
sed -i -r '/^[\s\t]*[#;]/d' <file>
# 2024-07-28
# The previous command does not remove comments from lines indented with spaces (for an unknown reason).
# So I remove these comments using another command below.
sed -i -r '/^\s+[#;]/d' <file>
sed -i -r '/^\s*$/d' <file>
For all files (matching a pattern) in the current folder and subfolders:
The current solution (2025-03-26)
find . \( -name '*.conf' -o -name '*.ini' \) -type f \
-exec sed -i -r \
-e '/^[[:space:]]*[#;]/d' \
-e '/^[[:space:]]*$/d' \
{} + \
-exec sed -i -z 's/\n$//' {} +
How to remove the trailing newline (LF
) character (the terminal newline, the EOF newline) from a text file using sed
?
The previous solution
find . -name '*.conf' -type f -exec sed -i -r '/^[\s\t]*[#;]/d' {} +
# 2024-07-28
# The previous command does not remove comments from lines indented with spaces (for an unknown reason).
# So I remove these comments using another command below.
find . -name '*.conf' -type f -exec sed -i -r '/^\s+[#;]/d' {} +
find . -name '*.conf' -type f -exec sed -i -r '/^\s*$/d' {} +