5/5/11

How to Remove Any Blank Lines Using Sed

The Unix "sed" command is shorthand for "Stream Editor," a program run on the command-line that modifies files automatically, based on provided search and replace patterns. You can use "sed" to search through a file for a piece of text or a regular expression, and then modify it with the text you want. If you have a file that has blank or empty lines, you may wish to parse it and remove these lines quickly to feed it into another program, or format the text in a way that you require. Sed is a standard utility that is built into all UNIX-like systems.
    • 1

      Log into your system with an account capable of writing to the file. This means that if it is a system file. you will need to use the root account; otherwise, log in as the owner or authorized user of the file.

    • 2

      Open a new terminal. Some distributions call this a "Command Line," while others call it a "Terminal" and some call it the "Shell."

    • 3

      Type the following command into the terminal: sed '/^$/d' /path/to/file.txt'

    • 4

      Change "/path/to/file.txt" to the name of the file that has the blank lines you would like to remove. This command works because the "/d" operator tells sed to delete text from a file while "/^$" represents a line that begins and ends without text.

  • No comments: