Live data from Hacker News

Batch editing files with ed

jvns.ca

11–20 of 78 posts

Re: Batch editing files with ed

#11
post #7

I've recently used EDLIN from an 80s version of MS-DOS. After spending 5 minutes with the manual, I realized it was the best line editor I've ever used. ed is very similar, but it doesn't come with a nice manual. The info page is chaotic and doesn't start from the beginning. You can cover typical usage in 5 lines, but no, you have to read through 25 pages of stuff just to figure out a sensible command. I just use vim…

My understanding is that man/info is meant more as a reference and less like a first time users guide.

Re: Batch editing files with ed

#12
post #6

There is also ex, which is sort of cousin of ed, but also closer to nowdays more familiar vi(m).

It's closer because vi was built on top of ex -- vi's ":" commands are just ex commands. In fact, on my Linux box, /bin/ex is just a symbolic link to /bin/vi:

    $ ls -l /bin/ex
    lrwxrwxrwx 1 root root 2 Oct  2  2017 /bin/ex -> vi
The original code for vi was written by Bill Joy in 1976, as the visual mode for a line editor called ex that Joy had written with Chuck Haley. Bill Joy's ex 1.1 was released as part of the first BSD Unix release in March 1978.[1]

(Bill Joy went on to become a co-founder of Sun Microsystems.)

"ex" stood for the extended version of ed.

[1] https://en.wikipedia.org/wiki/Vi

Re: Batch editing files with ed

#14
Nice article, good to hear ed is not dead. =)

You could also just add the text after the matching line. A little simpler and more straight forward.

    $ cat > /tmp/ed-script
    /baz
    a
      - elephants
    .
    w
    q
    $ cat /tmp/2
    foo:
      - bar
      - baz
      - bananas
    $ cat /tmp/ed-script | ed /tmp/2
    33
      - baz
    47
    $ cat /tmp/2
    foo:
      - bar
      - baz
      - elephants
      - bananas
    $·

Re: Batch editing files with ed

#15

   echo -e '/-baz\n+1\ni\n-elephant\n.\nw\nq\n'|ed foo
but ed requires a temp file in $TMPDIR to save changes

for speed, put $TMPDIR on memory file system

sed requires no temp file

   1.sed:
   /-baz/a\
   -elephant
   
   
   sed -f 1.sed foo|sed -a wfoo
works with all versions of sed, e.g., not all versions support "\n" in patterns nor so-called "edit-in-place" automatic temp file creation and removal

Re: Batch editing files with ed

#16

Nice article, good to hear ed is not dead. =) You could also just add the text after the matching line. A little simpler and more straight forward. $ cat > /tmp/ed-script /baz a - elephants . w q $ cat /tmp/2 foo: - bar - baz - bananas $ cat /tmp/ed-script | ed /tmp/2 33 - baz 47 $ cat /tmp/2 foo: - bar - baz - elephants - bananas $·

My guess is that the - is replacing something like an org-mode cookie, which could be in a few states that one might want to preserve.

Re: Batch editing files with ed

#20

echo -e '/-baz\n+1\ni\n-elephant\n.\nw\nq\n'|ed foo but ed requires a temp file in $TMPDIR to save changes for speed, put $TMPDIR on memory file system sed requires no temp file 1.sed: /-baz/a\ -elephant sed -f 1.sed foo|sed -a wfoo works with all versions of sed, e.g., not all versions support "\n" in patterns nor so-called "edit-in-place" automatic temp file creation and removal

Sure, there is more than one way to do X. It will be useful to learn why you prefer sed over ed
Post reply on HN