Forrest logo
back to the sed tool

file_manipulation:warp:e6754b24398f1b178804d18fadc5c82b

Delete empty lines in a file
$ sed '/^[[:space:]]*$/d' ${file_name}
try on your machine

The command:

sed '/^[[:space:]]*$/d' ${file_name}

  • "sed" stands for Stream EDitor, a command-line tool used for manipulating text.
  • '/^[[:space:]]*$/d' is the pattern or expression given to sed for matching lines.
  • "${file_name}" is the variable that represents the name of the file on which the sed command will operate.

Explanation:

  • The sed command is used here to delete (d) lines that match the given pattern or expression.
  • The pattern '/^[[:space:]]*$/d' can be broken down as:
    • '^' signifies the start of a line.
    • '[[:space:]]' represents any whitespace character.
    • '*' means zero or more occurrences of the preceding character.
    • '$' signifies the end of a line.
    • So, '/^[[:space:]]*$' matches any line that contains only whitespace characters or is entirely empty.
  • Therefore, the sed command will delete those lines from the specified file.
This explanation was created by an AI. In most cases those are correct. But please always be careful and never run a command you are not sure if it is safe.
back to the sed tool