Batch editing of text files?

Posted:
in Mac Software edited January 2014
I'm cracking my head against a wall trying to figure out how to do this. I want to take a whole bunch (few dozen) of text files, and make the same change in all of them. For example, insert "Hello World" into line 46. There must be some way of writing a nicely recursive shell script to do this, but I can't figure out how to edit the content of a file in a script. Can you give vi a set of instructions at launch on what to do to a file and have it automatically execute those instructiosns? How?



Thanks for any help!

Comments

  • Reply 1 of 7
    Try ed, or maybe vim in ex mode. See the man pages for actually making these work.
  • Reply 2 of 7
    baumanbauman Posts: 1,248member
    I'm sure sed or perl will do this for you... but I don't know them well enough to help. \
  • Reply 3 of 7
    Quote:

    Originally posted by bauman

    I'm sure sed or perl will do this for you... but I don't know them well enough to help. \



    sed is a better suggestion that my originals. awk is probably overkill for you. If the man pages don't give you enough to go on, O'Reilly publishes "sed & awk" which is very good.



    Just thinking about this involves shaking off the rust! It's been a long time since I used either, but "sed & awk" is what helped me out back then. Good luck.
  • Reply 4 of 7
    toweltowel Posts: 1,479member
    sed looks like just what I need. Now gotta digest some man pages. Thanks!



    Edit: w00t! Just wrote my first functional shell script containing my first functional sed calls. This stuff is so cool. 8)
  • Reply 5 of 7
    vim lets you record macros for when you have to do something too often to do it by hand, but not enough to write an actual script.



    There's an entire online manual here:

    http://www.newriders.com/books/opl/e...735710015.html



    Note that vim is included with OS X as of Panther rather than just vi
  • Reply 6 of 7
    How about using BBEdit or some other text editor? sounds a lot easier to me. Don't have it? Send them to me and I'll send them right back with the change.



    Am I missing something?

    Chas
  • Reply 7 of 7
    toweltowel Posts: 1,479member
    Quote:

    Originally posted by Macmedia

    How about using BBEdit or some other text editor? sounds a lot easier to me. Don't have it? Send them to me and I'll send them right back with the change.



    Am I missing something?

    Chas




    Hmm. Looks like BBEdit is applescriptable. So yeah, you could write an applescript with BBEdit to accomplish the same thing. That's a good idea, if not quite as geekalicious as a shell script.



    I just hate doing highly repetitive tasks by hand. I'd rather spend a whole day (or three) learning how to automate the task than spend the two or three hours just doing it by brute force. But then, when I discover I forgot one quotation mark in every file, it takes only seconds, instead of another three hours, to do the whole process again. And I've discovered that I never, ever do things right the first time.
Sign In or Register to comment.