Live data from Hacker News

Batch editing files with ed

jvns.ca

51–60 of 78 posts

Re: Batch editing files with ed

#51

Good to know about ed! Since noone else has mentioned this, emacs' keyboard macros seem much easier to me especially since more than the basic editing stuff off awk/ed/sed i can leverage all the editor extensions and modifications i have accumulated over the years. That is unless its tens of thousands of files and the edit is exceptionally simple. I would write a script in that case too.

Since discovering them, I use Emacs keyboard macros all the time. Let's say I have: key value salt pepper fish chips vodka orange rum cola and I want the second column in uppercase. to start recording a macro. Alt →, → to position the cursor at "v" (or just →→→→→→ if this is a fixed width column), then Alt U to uppercase the next word. → to move the cursor one forward, to the start of the next line. to finish recordi…

Microoptimizing a couple of things, I'd do this from the top of the buffer:

     M-f M-u C-n C-a C-0 
I particularly like combining the "finish recording" and "running the macro" steps into a single . Plus, using a numeric argument of 0 seems better than counting lines.

Re: Batch editing files with ed

#52
post #31

Earlier quoted context omitted.

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

Why write the ‘ex’ commands to a file? Why not just do the echo inline, like this for f in *.html; do echo -e "g/search string/ .,+20 d\nx" | ex - "$f" done ? (I also added quotes to the $f dereference in case of file names containing white space, and the -e flag to echo to expand \n to newline. In case of a Bourne shell without support for -e in echo, I would probably use “{ echo "g/..."; echo "x"; } | ex - ...” ins…

Thanks for the tips. I put them in a file, because I saw someone using ed last year in a script and started looking at ex from there.

    diff -e file1 file2 > ed_script
Using echo (along with your other suggestions) is probably better for that example.

Re: Batch editing files with ed

#53
It's worth noting that the "enter a single . on a line to signal the end of an input" convention found its way into mail/mailx and SMTP too. The good thing is that it means you don't need to insert special characters like Esc (Ctrl+[, 27, 0x1B, whatever you want to call it) into your script; the bad thing is when you do want to add a line containing a single "."... whereby ed and SMTP have diverged with different "escaping" conventions.

Re: Batch editing files with ed

#54

Earlier quoted context omitted.

Since discovering them, I use Emacs keyboard macros all the time. Let's say I have: key value salt pepper fish chips vodka orange rum cola and I want the second column in uppercase. to start recording a macro. Alt →, → to position the cursor at "v" (or just →→→→→→ if this is a fixed width column), then Alt U to uppercase the next word. → to move the cursor one forward, to the start of the next line. to finish recordi…

The equivalent in vim is: • qq to start recording a macro in register q, • w to jump to the second word, • gUaw to "go uppercase a word", • j to move to next line (↓ works as well), • q to stop recording, • 5@q to apply macro in register q five times. But in this example I would have probably used ex command: :%normal wgUaw (for every line do as if I had typed wgUaw) or visualy selected the second column as a block a…

You can do it using multiple cursors. On Sublime, you place the cursor on beginning of “value”, then press Ctrl+Shift+Down until the end - there will be a cursor on every line. Then you press Ctrl+Right to select all values on the second column. Then press Ctrl+P and choose “Convert to Upper Case”, or just Ctrl+KU.

Re: Batch editing files with ed

#55

Awk really is the tool of choice for this sort of thing: $ awk '{print}/baz/ {sub("baz", "elephant"); print}' jvns.txt foo: - bar - baz - elephant - bananas Since the script is single quoted you can also lay it out legibly: $awk ' {print} /baz/ { sub ("baz", "elephant") print } ' which is nice for more complex "one liners". Awk is also standard on all posix environments and in the mawk flavor is extremely fast (relev…

(s)ed seems just as good, if not better (depending on size of workload).

also, this and most other examples itt don't solve the other part of the problem: saving the changes. ofc, the author provides a solution.

Re: Batch editing files with ed

#56
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…

> ed is very similar, but it doesn't come with a nice manual

In many case it indeed doesn't. Historically, that documentation void was meant to be filled by learn tutorials[1] and a written introduction in volume 2A of the manual[2,3]. Unfortunately, the learn tutorials can't really be made use of in a modern environment; you’d actually have to set up a PDP-11 emulator with V7 (though prebuilt images exist) and work with that, an environment where backspace doesn’t really work out of the box.

OpenBSD ed(1)[4] tries, but it's just not quite there as an introduction.

[1] http://a.papnet.eu/UNIX/v7/files/doc/07_learn.pdf

[2] http://a.papnet.eu/UNIX/v7/files/doc/04_edtut.pdf

[3] http://a.papnet.eu/UNIX/v7/files/doc/05_adved.pdf

[4] https://man.openbsd.org/ed.1

Re: Batch editing files with ed

#57
There is another program I use for editing that is older than ed. It is written in asm. I think it may actually be faster than sed (and sed is faster than AWK, Lua, Perl, Python, etc.)

  1.spt:

  ; x = "  - baz" 
  ; y = "  - elephant"
  ;a a = input :f(end)
  ;  output = a
  ;  a ? x :s(d)f(a) 
  ;d output = y
  ; :(a)
  ;end

  spitbol 1.spt 
Post reply on HN