Say you have a text file. You need to alter it in some regular way before sending it on somewhere else. Instead of editing by hand, there is a neat option you can use to edit the file with familiar vim commands (instead of awk’s lovely syntax), without having to open vim interactively.
The principle is simple: You make a file containing each of the commands you want to run, one per line. They will be the same form as if you typed them in a vim session, starting with “:”. Make sure that “:wq” is the last one. Then you run vim with:
vim -s FILEOFCOMMANDS.txt FILETOEDIT.txt
Example case. My file to edit is tester.txt, contents being:
64.114.33.23,www.example.com 64.114.33.24,www.example.co.uk 64.114.33.25,www.example.co.jp 64.114.33.26,www.example.co.fr 64.114.33.27,www.example.co.cz
Then I have a
1 | commands.txt |
file containing:
:%s/^/("
:%s/,/","
:%s/$/"),
:wq
Finally I ran it as
1 | vim -s commands.txt tester.txt |
. As a result,
1 | cat tester.txt |
:
("64.114.33.23","www.zenoss.com.co.uk"),
("64.114.33.24","www.zenoss.com.co.de"),
("64.114.33.25","www.zenoss.com.co.fr"),
("64.114.33.26","www.zenoss.com.co.cp"),
("64.114.33.27","www.zenoss.com.co.jp"),
You could put something like this in a cron job, making transitions between data manipulation steps quick and easy, without having to mess with the files by hand each time.
No related posts.
Related posts brought to you by Yet Another Related Posts Plugin.









Very nicely put…now the $64 question is, how do you tell VI not to display any output while running these scripts?
I can redirect output to another terminal, but it still wants to write to stdout.
I tried redirecting stdout to /dev/null, and a few other permutations, always ended up getting “Vim: Warning: Output is not to a terminal”. Not sure how to get around that yet.
I think you could use the script command ( http://www.sun.com/bigadmin/content/submitted/handle_terminal_output.jsp ), and redirect output to another display, then close it in your original script, without the originating shell ever being the wiser.