如何在 Gvim 或 vi 中将文本附加到行尾

发布于 2024-12-17 01:57:04 字数 968 浏览 3 评论 0原文

我在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

盛夏已如深秋| 2024-12-24 01:57:04

您需要进行一点反向引用。
将搜索词更改为:
\(\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 replace
Add \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

流心雨 2024-12-24 01:57:04

如果您选择一个块(使用 CTRL+V (认为如果您在 Windows 中,这可能会给您带来麻烦),您可以使用以下命令将文本附加到块中的所有行A

例如,假设我选择了下面粗斜体的代码:


    Lorem ipsum dolor sit amet, consectetur adipisicing elit, 
    sed do eiusmod tempor incididunt ut labore et dolore magna
    aliqua. Ut enim ad minim veniam, quis nostrud exercitation 
    ullamco laboris nisi ut aliquip ex ea commodo consequat. 
    Duis aute irure dolor in reprehenderit in voluptate velit 
    esse cillum dolore eu fugiat nulla pariatur. Excepteur sint
    occaecat cupidatat non proident, sunt in culpa qui officia
    deserunt mollit anim id est laborum.

现在我输入 A。 > 和文本 bla 后跟 ESC

结果是:


    Lorem ipsum dolor sit amet, coblansectetur adipisicing elit, 
    sed do eiusmod tempor incididublant ut labore et dolore magna
    aliqua. Ut enim ad minim veniablam, quis nostrud exercitation 
    ullamco laboris nisi ut aliquiblap ex ea commodo consequat. 
    Duis aute irure dolor in repreblahenderit in voluptate velit 
    esse cillum dolore eu fugiat nblaulla pariatur. Excepteur sint
    occaecat cupidatat non proidenblat, sunt in culpa qui officia
    deserunt mollit anim id est lablaborum.

同样,如果您键入 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:


    Lorem ipsum dolor sit amet, consectetur adipisicing elit, 
    sed do eiusmod tempor incididunt ut labore et dolore magna
    aliqua. Ut enim ad minim veniam, quis nostrud exercitation 
    ullamco laboris nisi ut aliquip ex ea commodo consequat. 
    Duis aute irure dolor in reprehenderit in voluptate velit 
    esse cillum dolore eu fugiat nulla pariatur. Excepteur sint
    occaecat cupidatat non proident, sunt in culpa qui officia
    deserunt mollit anim id est laborum.

And now I type A and the text bla followed by ESC.

The result is:


    Lorem ipsum dolor sit amet, coblansectetur adipisicing elit, 
    sed do eiusmod tempor incididublant ut labore et dolore magna
    aliqua. Ut enim ad minim veniablam, quis nostrud exercitation 
    ullamco laboris nisi ut aliquiblap ex ea commodo consequat. 
    Duis aute irure dolor in repreblahenderit in voluptate velit 
    esse cillum dolore eu fugiat nblaulla pariatur. Excepteur sint
    occaecat cupidatat non proidenblat, sunt in culpa qui officia
    deserunt mollit anim id est lablaborum.

Similarly, if you type I instead of A, the text will be prepended.

风吹过旳痕迹 2024-12-24 01:57:04

您始终可以录制修改一行并移至下一行的宏,然后重播该宏:

qq$bywA', count(count(*) from <ESC>pAwhere msgid in (select msgid from mainTable where row_dtm<'17NOV11')<ESC>jq2@q
  • 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$bywA', count(count(*) from <ESC>pAwhere msgid in (select msgid from mainTable where row_dtm<'17NOV11')<ESC>jq2@q
  • qq → start recording a macro in register q.
  • $ → 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 register q 2 times.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文