5/5/11

How to Add a Newline Character to the End of a Line in Sed

Sed is a special type of text file editor known as a "stream editor." Rather than editing text files, sed applies edits to a stream of text according to instructions given to it by the user. The stream of text could come from anywhere: the user, the Internet or a local file. You can imagine sed to be a bit like a programming language designed solely for manipulating the content of text and reformatting it into a structure you like. In the hands of a skilled user, it can remove much of the monotony of routine work, however it can be a bit intimidating at first, so a good start is a simple edit: adding a newline character to the end of each given line.
    • 1

      Open a terminal. The procedure to do this will depend on your operating system. Most Unix and Linux systems either open a terminal immediately on start up or have a terminal icon somewhere on the desktop. In Mac OS X, click the "Spotlight" button and type "Terminal." In Windows, click "Start," "Run" and type "cmd."

    • 2

      Type the following :

      sed '/$/G'

      This tells sed to search the text for all end of lines ($) and add a new line (G), effectively double-spacing the file. Alternatively, if you want to insure that there is never more than one blank line between paragraphs, change the command to read:

      sed '/^$/d;G'

      This tells sed to match only completely blank lines (^$), delete those blank lines (d) and replace them with a single new line (G).

    • 3

      Feed the data from a text file into sed by typing the following:

      cat textfile.txt | sed '/^$/d;G'

  • No comments: