Live data from Hacker News

Batch editing files with ed

jvns.ca

21–30 of 78 posts

Re: Batch editing files with ed

#22
This problem is easily solved with a regexp that processes the input as a whole, rather than working on it line by line. I had this need often enough that I exported Go regexp engine as a command line tool regrep, which can insert "elephant" after "baz" with:

  go get github.com/orivej/unix/regrep
  regrep s '(\n( *-) baz\n)' $'$1$2 elephant\n' 
It only processes standard input, and can not by itself replace the contents of an input file with its output; but another tool, inplace, helps:

  go get github.com/orivej/unix/inplace
  find . -name '*.yaml' -exec inplace {} regrep s '(\n( *-) baz\n)' $'$1$2 elephant\n' \;

Re: Batch editing files with ed

#23

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 $·

I don’t think matches the spec that the new line has the same number of leading spaces as the surrounding lines

Re: Batch editing files with ed

#24

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 $·

I don’t think matches the spec that the new line has the same number of leading spaces as the surrounding lines

Weird, after rereading the article, it seems like I may have imagined that part.

Re: Batch editing files with ed

#26
Pretty much this whole thread is people coming up with better ways of solving the problem. I love Hacker News! :) But I think the point of the blog (for me) is showing what 'ed' is and where can you use it for (the problem at hand is secondary). And it did exactly that for me, I knew about 'ed' but never really understood what made it special or where/how you could use it. Thanks Julia for enlightening me!

Re: Batch editing files with ed

#27

Earlier quoted context omitted.

I don’t think matches the spec that the new line has the same number of leading spaces as the surrounding lines

Weird, after rereading the article, it seems like I may have imagined that part.

An older version of the article contained the following:

> I had one extra weird requirement which was that some of the lines were indented with 2 spaces, and some with 4 spaces. The - elephant line needed to have the same indentation as the previous line.

Re: Batch editing files with ed

#28
post #4

In practice I often use Vim instead. :args file1.txt file2.txt file3.txt :set autowrite :argdo norm /- baz/ yypwCelephants The commands set the argument list to a list of three files (by default, it is set to the filenames you passed to vim on the command-line). Then, autowrite is enabled which automatically saves each buffer after editing it. Finally, argdo runs a command on each argument file.

Thanks for that tip. I've used ex for similar things when editing hundreds of files.

This example searches each HTML file in a directory for a line with a string and then deletes a number of lines:

    $ echo "g/search string/ .,+20 d\nx" >> exscript
    $ for f in *.html
      do
          ex - $f 

Re: Batch editing files with ed

#29
post #22

This problem is easily solved with a regexp that processes the input as a whole, rather than working on it line by line. I had this need often enough that I exported Go regexp engine as a command line tool regrep, which can insert "elephant" after "baz" with: go get github.com/orivej/unix/regrep regrep s '(\n( *-) baz\n)' $'$1$2 elephant\n' It only processes standard input, and can not by itself replace the contents…

It's close to the idea of structural regular expressions [0]. I'm still waiting for the awk from the paper.

[0] http://doc.cat-v.org/bell_labs/structural_regexps/

Re: Batch editing files with ed

#30
I do this fairly regularly in various shell scripts, but less now than previously ever since “sed” introduced the --in-place option, making it more useful for my purposes most of the time.
Post reply on HN