Live data from Hacker News

Batch editing files with ed

jvns.ca

41–50 of 78 posts

Re: Batch editing files with ed

#41

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 recording the macro.

Then press five times to run the macro five times.

(Explanation intended for users who've never used Emacs before. Of course, there are optimizations.)

Re: Batch editing files with ed

#42
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.

Good man pages can be great first time user guides.

Re: Batch editing files with ed

#43

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

The sed command doesn't get the indentation right, though, as the article says it could be indented by two or four spaces.

   1.sed:
   s/- baz/- elephant/;
   /^  - elephant/{h;G;}
   /^    - elephant/{h;G;}
   s/- elephant/- baz/;

   sed -f 1.sed foo|sed -a wfoo
or

   1.sed:
   /^  - baz/a\
     - elephant
   

   /^    - baz/a\
       - elephant
   

   sed -f 1.sed foo|sed -a wfoo

Re: Batch editing files with ed

#44

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…

The AWK Programming Language is the best programming book ever because it lets you learn the language through interesting problems (like writing a very small assembler).

Re: Batch editing files with ed

#47

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…

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 and just pressed U.

I'm genuinely interested in someone showing how to do this kind of trasformation in popular modern editors such as Atom and VSCode. Is there such a flexible way as in the classic editors?

Re: Batch editing files with ed

#48

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…

The AWK Programming Language is the best programming book ever because it lets you learn the language through interesting problems (like writing a very small assembler).

It's available online. Thanks for the tip.

https://ia802309.us.archive.org/25/items/pdfy-MgN0H1joIoDVoI...

Re: Batch editing files with ed

#49

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…

The main reason i am not moving to something more trendy are kbd macros and the fact that emacs is designed to be equal parts runtime and editor. I think that for most purposes emacs and vim are equivalent but virtually all modern editors are lacking compared to a well good emacs/vim configuration.

Re: Batch editing files with ed

#50
Chapter 20 of O'Reilly's "Unix Power Tools, 3rd Edition" is all about batch editing and covers ed/ex as well.

Maybe there's an old copy of "Unix Power Tools" over in your server room or an abandoned cubicle in the office... the content has not changed much in the ensuing decades!

Post reply on HN