How to use the original unix ed editor?



  • If you're old school, you may be thinking when was the last time I used ed? However for beginners it is good to know it exists and is one of the initial editors. You're probably glad you have vim or nano now.

    It's still on all unix & linux operating systems, so another reason to know about it. Some of the syntax still applies to vi & vim, so should be handy. It is also the basis of using regular expressions.

    ed is a line editor you work on one line at a time, it has not prompt, and if you enter a command it does not understand it prints a "?" question mark.

    My file contains the following:

    $ cat line_editor 
    Hello, nice to meet you ed!
    Hello, nice to meet you ed! line 2
    Hello, nice to meet you ed! line 3
    

    The file contain 28 characters

    $ ed line_editor 
    28
    

    Print the first line in the file.

    $ ed line_editor 
    28
    p
    Hello, nice to meet you ed!
    

    With ed you move to the line you need to work one by specifying the line number, this deletes line 2.

    $ ed line_editor 
    98
    2
    Hello, nice to meet you ed! line 2
    d
    

    A short way of doing the above is typing "1d"

    Rather than have a number match the line, you can also have a word.
    so we have the following file, the 1,$p is a range print lines 1 through $, $ meaning end of the file.

    $ ed line_editor 
    98
    1,$p
    Hello, nice to meet you ed!
    Hello, nice to meet you ed! line 2
    Hello, nice to meet you ed! line 3
    

    Let's delete line with containing "line 3" with the delete command "d". Remember it deletes the entire line not just the regular expression.

    $ ed line_editor 
    98
    /line 3/d
    1,$p
    Hello, nice to meet you ed!
    Hello, nice to meet you ed! line 2
    

    The above only deletes the specific line, containing "line 3", if we wanted to delete everything with the word "line", you can use the global command "g". You can see the line are delete, by printing all remaining lines.

    $ ed line_editor 
    98
    g/line/d
    1,$p
    Hello, nice to meet you ed!
    

    Substitution Command

    The command takes the form of:
    [address]s/pattern/replacement/flag

    Before starting some important notes.
    The g at the beginning means global on the whole file.
    The g at the end means act on one line.

    Command Description
    s/line/thinline/ Replace first occurrence of line with thinline, on current line
    s/line/thinline/g Replace all occurrences of line with thinline only on that line
    /walk/s/line/thinline/g This uses the address /walk/, finds the first line with that word, and then substitutes "thin" for "thinline", just on one line.
    g/walk/s/line/thinline/g Does the same as above but because of the leading "g", on all the lines.
    g/walk/s//thinline/g If the address is "walk" and the word to substitute are the same just put in "//" two slashes.

    You can pass a script with a number of ed commands to edit a file
    Here is the file to edit

    $ cat line_editor 
    Hello, nice to meet you ed!
    Hello, nice to meet you ed! line 2
    Hello, nice to meet you ed! line 3
    

    Here is the script

    $ cat ed_script1 
    g/line/s//thinline/g
    1,$p
    

    Running the script

    $ ed line_editor < ed_script1 
    98
    Hello, nice to meet you ed!
    Hello, nice to meet you ed! thinline 2
    Hello, nice to meet you ed! thinline 3
    ?
    

    You will notice the above does not write the changes, to save the changes, add a "w" write, "q" quit commands to your script, on separate lines.

    g/line/s//thinline/g
    1,$p
    w
    q
    

Log in to reply
 

© Lightnetics 2024