views:

35

answers:

5

I run into situations where I have to repeat a single line of text with some minor editing.

For example:
1. insert into table_name (col_one, col_two) values (val_1, val_2);
2. insert into table_name (col_one, col_two) values (val_3, val_4);
3. insert into table_name (col_one, col_two) values (val_5, val_6);
and so on... (1000 records)

I mean, I will only type the first query and write a macro using the text editor to generate the required number of queries. Hope you get the picture.

Please suggest a better text editor for this purpose. As I have already tried and failed with Ultraedit/Textpad. Maybe I am doing something wrong, as I cudnt get Vim to do this either.

A: 

Choices would be Vim or Emacs. Eg. Emacs offer macros and regular expressions, so you should be able to do what you want. Might be non-obvious though.

I am not very comfortable with Vim/Emacs. Can you send me a link please ?
A: 

I think the problem might be more easily solved if you use a shell script to generate the text, rather than macros in a text editor.

pavium
A: 

An editor is probably the wrong tool here - if you want to generate all those insert statements, you are probably better off writing a small program in a scripting language like perl or python.

anon
that is what I am doing now, using python. But it is a quite generic problem. There must be an even quicker way.
A: 

Just about any text editor will let you copy lines and edit them, certainly vim.

The right way to tackle this is to do this programatically: create a template file that generates the text file you want. m4, a widely used macro-language is well-suited to this task, since you can mix text and macros freely. Cf. http://en.wikipedia.org/wiki/M4_(computer_language)

E.g. (untested)

define(`txt`, "$1. insert into table_name (col_one, col_two) values (val_$2, val_$3);")
forloop(`i',1,1000,txt(i,eval(2*i-1),eval(2*i)))
Charles Stewart
+1  A: 

For the example given where only the numbers change, this should be possible using the Zeus editor and a macro script. For example this numbers script does something very similar.

To achieve the required result take the numbers script from above and make some minor changes to create an odd numbers and even numbers script.

Then column mark the 1000 lines of the odd column and run the odd numbers script. Then column mark the 1000 lines of the even column and run the even numbers script.

jussij