如何在 Gvim 或 vi 中将文本附加到行尾
我在 Windows 上使用 gvim,并且有一个包含几行的文件,如下所示,
Select '305','TableA
Select '313','TableB
Select '313','TableC
我想在每行末尾附加以下行。
', count(*) from [Tablename] where msgid in (select msgid from mainTable where row_dtm<'17NOV11')
结果应该看起来像这样(请注意,[TableName] 被实际的表名称替换。
Select '305','TableA', count(*) from TableA where msgid in (select msgid from mainTable where row_dtm<'17NOV11')
Select '313','TableB', count(*) from TableB where msgid in (select msgid from mainTable where row_dtm<'17NOV11')
Select '313','TableC', count(*) from TableC where msgid in (select msgid from mainTable where row_dtm<'17NOV11')
我可以通过执行在末尾添加附加文本
%s/$/', count(*) from [Tablename] where msgid in (select msgid from mainTable where row_dtm<'17NOV11')/g
但我不知道如何读取表名称并把它基本上
我只是想生成几个查询。 我知道表名称始终是从行尾到第一个 ' 字符的文本,但我如何读取它并将其添加到修改后的字符串中?
谢谢
I am using gvim on windows and have a file with several lines that look like this
Select '305','TableA
Select '313','TableB
Select '313','TableC
I would like to append the following line at the end of each line.
', count(*) from [Tablename] where msgid in (select msgid from mainTable where row_dtm<'17NOV11')
The result should look something like this (Note that the [TableName] is replaced by the actual table name.
Select '305','TableA', count(*) from TableA where msgid in (select msgid from mainTable where row_dtm<'17NOV11')
Select '313','TableB', count(*) from TableB where msgid in (select msgid from mainTable where row_dtm<'17NOV11')
Select '313','TableC', count(*) from TableC where msgid in (select msgid from mainTable where row_dtm<'17NOV11')
I can add append the text at the end by just doing
%s/$/', count(*) from [Tablename] where msgid in (select msgid from mainTable where row_dtm<'17NOV11')/g
But i am stuck as to how i can read the table name and put that in place of [Tablename].
Basically i am just trying to generate several queries. Any ideas?
I know that the table name will always be the text from the end of the line to the first ' character but how do i read it and add it to the modified string?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您需要进行一点反向引用。
将搜索词更改为:
\(\a\+\)$
这将与表名称匹配。用\(
和\)
包围搜索词允许在替换中引用它将 \1 添加到替换字符串中您希望表名出现的位置。
结果应该是这样的
%s/\(\a\+\)$/\1', count(*) from [\1] where msgid in (select msgid from mainTable where row_dtm<'17NOV11')/g
You need to do a tiny bit of back referencing.
Change the search term to:
\(\a\+\)$
this will match the table name. Surrounding the search term with\(
and\)
allow it to be referenced in the replaceAdd \1 to your replacement string wherever you want the tablename to appear.
The result should look like this
%s/\(\a\+\)$/\1', count(*) from [\1] where msgid in (select msgid from mainTable where row_dtm<'17NOV11')/g
如果您选择一个块(使用 CTRL+V (认为如果您在 Windows 中,这可能会给您带来麻烦),您可以使用以下命令将文本附加到块中的所有行A
例如,假设我选择了下面粗斜体的代码:
现在我输入 A。 > 和文本
bla
后跟 ESC结果是:
同样,如果您键入 I 而不是 A,则文本将被前置。
If you select a block (using CTRL+V (thought that may give you trouble if you're in Windows), you can append text to all lines in the block using A.
For instance, assume that I selected the code that is in bold italics below:
And now I type A and the text
bla
followed by ESC.The result is:
Similarly, if you type I instead of A, the text will be prepended.
您始终可以录制修改一行并移至下一行的宏,然后重播该宏:
qq
→开始在寄存器q
中录制宏。$
→ 转到行尾。b
→ 返回到上一个单词的开头。yw
→ 复制单词。Afoo
→ 在行尾附加“foo”,使用
键结束插入模式。p
→ 粘贴拉出的单词。Afoo
→ 在行尾附加“foo”,使用
键结束插入模式。j
→ 转到下一行。q
→ 结束宏录制。2@q
→ 重放寄存器q
中的宏2次。You can always record a macro modifying a line and moving to the next, and then replay that macro:
qq
→ start recording a macro in registerq
.$
→ go to end of line.b
→ go back to the beginning of the previous word.yw
→ yank word.Afoo<ESC>
→ append "foo" at end of line, end insert mode with<ESC>
key.p
→ paste yanked word.Afoo<ESC>
→ append "foo" at end of line, end insert mode with<ESC>
key.j
→ go to the next line.q
→ end macro recording.2@q
→ replay macro in registerq
2 times.