1. 14
  1.  

  2. 2

    Hells yes!

    I am still most comfortable by far doing my text manipulation scripting in ed. Why? Because I can sit there and interactively try things out and put the commands together. IMO that’s huge.

    1. 1

      I want to know more!

      Could you share an example of how you work with ed?

      1. 4

        OK, let’s say you have a simple text file you need to edit under script control:

        cpatti@dev-dsk-cpatti-1e-5d911563:03:02 PM:~
        $ cat test.txt
        How many cans can a canner can if a canner can can cans?  A canner can can as many cans as a canner can if a canner can can cans.
        cpatti@dev-dsk-cpatti-1e-5d911563:03:02 PM:~
        

        Let’s say you want to write a Limerick transmogrification script :)

        You can fire up ed, interactively twiddle until you find the right transformations (Note that ed uses the same basic commands as vi’s command mode, vi contains ex, which is a descendant of ed):

        Here’s my session for this example:

        cpatti@dev-dsk-cpatti-1e-5d911563:02:47 PM:~
        $ ed test.txt
        130
        s/many/much/g
        s/canner/woodchuck/g
        .
        How much cans can a woodchuck can if a woodchuck can can cans?  A woodchuck can can as much cans as a woodchuck can if a woodchuck can can cans.
        s/cans/wood/g
        .
        How much wood can a woodchuck can if a woodchuck can can wood?  A woodchuck can can as much wood as a woodchuck can if a woodchuck can can wood.
        s/woodchuck can/woodchuck chuck/g
        .
        How much wood can a woodchuck chuck if a woodchuck chuck can wood?  A woodchuck chuck can as much wood as a woodchuck chuck if a woodchuck chuck can wood.
        s/woodchuck chuck can/woodchuck could chuck/g
        .
        How much wood can a woodchuck chuck if a woodchuck could chuck wood?  A woodchuck could chuck as much wood as a woodchuck chuck if a woodchuck could chuck wood.
        

        Now that I have the commands I want to run, I can embed them in a here document in my script, or as I’m doing tin this case, put them into a file so I can do multiple invocations without violating DRY:

        cpatti@dev-dsk-cpatti-1e-5d911563:02:54 PM:~
        $ cat ed
        s/many/much/g
        s/canner/woodchuck/g
        s/cans/wood/g
        s/woodchuck can/woodchuck chuck/g
        s/woodchuck chuck can/woodchuck could chuck/g
        s/as a woodchuck chuck/as a woodchuck could/g
        

        Now let’s run our script and examine our output:

        cpatti@dev-dsk-cpatti-1e-5d911563:03:02 PM:~
        $ cat ed | ed -v test.txt
        130
        161
        cpatti@dev-dsk-cpatti-1e-5d911563:03:03 PM:~
        $ cat test.txt
        How much wood can a woodchuck chuck if a woodchuck could chuck wood?  A woodchuck could chuck as much wood as a woodchuck could if a woodchuck could chuck wood.
        

        Now, sure, you could do EXACTLY this with a sed invocation, or with awk, or myriad other ways, but being able to interactively build the edit is incredibly powerful and can lead to super simple development of some very powerful text transformations.

        I feel a blog post coming on :)

        1. 1

          Oops. One error in that ed script I fixed but wasn’t there when I ‘cat’ted the file - You have to have a ‘w’ at the end or A) the changes won’t write and B) ed will whine with its ever helpful ‘?’ error :)

    2. 1

      Serious question: As someone whose never used ed but uses vim on a daily basis, is it worth learning ed? I’m not a regex master by any means but I feel like I grok them enough that I wouldn’t want to learn ed as a way to get better at regexes (emphasized in the description).

      1. 1

        No, I don’t think learning ed will make you better at regexes. For me it’s helpful to know if you ever find yourself in an environment where you don’t have access to other tools like vi. Ie, the OpenBSD install media or possible single user mode on some gnu/linux distro.

      2. 1

        Just bought my copy!

        1. 1

          I’m going to buy it tomorrow… Just to make sure.