Batch editing of text files?
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!
Thanks for any help!
Comments
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.
Edit: w00t! Just wrote my first functional shell script containing my first functional sed calls. This stuff is so cool. 8)
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
Am I missing something?
Chas
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.