Yes, I know
How to perform basic text editing using Sed

It is not uncommon to have to apply similar changes to a set of many different files. For example, a programmer might want to insert some license text on top of each source file of a project. Or one might want to update the copyright notice on all pages of a static website.

 Get the support materialCheck your mailbox for the link!  View on YouTube 

How to perform basic text editing using Sed

Doing such tasks by hand would be terribly time-consuming …​ and awfully boring. Fortunately, when working with text files, the standard Unix tool chest has all that is required to automate that work.

In this video, I will use one of the most versatile non-interactive text editor: The sed Stream Editor.

How can I take the most of this video?

I encourage you to download the files used on the video so you will be able to try the same commands as me on your own system.

The link above will allow you to download a gzipped tar archive of the files used in the video. To extract the content of that archive on your system, you can use the command:

    tar xzf Yes_I_Know_IT-Ep07.tar.gz

In addition, as a memory aid, you might want to take a look at my Sed Cheatsheet which summarizes the various sed commands and syntaxes used in this video.

What is the target audience?

This video is clearly aimed toward new shell users. The video was designed on a Linux system and using the Bash shell. But the features demonstrated here are generic enough to work with other shells and/or other Unix-like OS (*BSD, MacOSX, …​).

Please notice I’ve made use of some GNU sed extension in this video. Formally:

  • The -i option to make changes in-place is not part of POSIX. BSD versions of sed usually require a backup extension after the -i option. Other sed implementations do not have at all the -i option and require you to create a temporary file yourself. As a comparison:

    # Delete the last line of a file:
    # GNU sed
    sed -i '$d' some.file
    # BSD sed
    sed -i BAK '$d' some.file
    # POSIX sed
    sed '$d' some.file > some.file.NEW
    mv -f some.file.NEW some.file
  • In a strictly POSIX compliant sed, the a and i commands must be terminated by a backslash and the the new text has to be on its own line. On the other hand, GNU sed allows the new text to appear right after the command:

    # Insert C-style comments before and after a code snippet:
    # GNU sed
    sed -e '1i/*' -e '$a*/' code.snippet
    # POSIX sed, possibly BSD sed
    sed -e '1i\
    /*' -e '$a\
    */' code.snippet

What are the requirements?

  • You should have a working Unix-like system.

  • You should know how to open a terminal window.