【问题标题】:How to find/replace a character in every specific line如何在每个特定行中查找/替换字符
【发布时间】:2020-07-04 10:06:39
【问题描述】:

假设我有 500,000 行以逗号 (,) 结尾,如下所示:

test,'number',null,
test,'number',null,
test,'number',null,

我想在例如每 50,000 行的末尾用 (;) 查找/替换逗号 (,):

line 1:      test,'number',null,
line 50:     test,'number',null,
line 50,000: test,'number',null;

这可能与nodepad++或emeditor有关吗?

提前致谢

【问题讨论】:

    标签: notepad++ emeditor


    【解决方案1】:
    • Ctrl+H
    • 查找内容:^(?:[^\r\n]*,\R){49999}[^\r\n]+\K,$
    • 替换为:;
    • 检查 环绕
    • CHECK 正则表达式
    • 全部替换

    说明:

    ^                   # beginning of line
      (?:               # non capture group
        [^\r\n]*        # 0 or more any character but newline
        ,               # a comma
        \R              # any kind of linebreak
      ){49999}          # end group, must appear 49999 times
      [^\r\n]+          # 1 or more non linebreak
      \K                # reset
      ,                 # a comma
    $                   # end of line
    

    例如,我有 3 行而不是 50000

    屏幕截图(之前):

    截图(之后):

    【讨论】:

    • 它适用于nodepad++,但不适用于emeditor,那又如何呢?因为我有一个大文件
    • @Michael:我不知道emeditor,可能不支持\K,你可以试试:查找:^((?:[^\r\n]*,\R){49999}[^\r\n]+),$替换:$1;\1;跨度>
    • 在 emeditor 中不起作用,你能试试吗,因为我有一个很大的 sql,我无法在 nodepad++ 中打开它,我也试过 nodepad++ {49999} 不能在 15000 下工作像 {14300} 一样有效。
    • @Michael:perl oneliner 可以完成这项工作:perl -pe 's/,$/;/ unless $.%50000' inputfile。之前做好备份!
    • EmEditor中,点击替换对话框中的高级按钮,在中输入50000 b>搜索正则表达式的附加行文本框。
    【解决方案2】:

    EmEditor 中,您可以在 500,000 行文档处于活动状态时运行此宏。为此,请将此代码另存为 ReplaceEvery50000.jsee,然后从 Macros 菜单的 Select... 中选择此文件。最后,打开您的 500,000 行文档,并在您的 500,000 行文档处于活动状态时,在 Macros 菜单中选择 Run

    editor.ExecuteCommandByID(4472);  // ensure non-CSV mode
    yLines = document.GetLines();
    for( y = 50000; y <= yLines; y += 50000 ) {
        s = document.GetLine( y );
        x = s.length;
        if( s.substr( x - 1, 1 ) == "," ) {
            document.selection.SetActivePoint( eePosLogical, x, y );
            document.selection.SetActivePoint( eePosLogical, x + 1, y, true );
            document.selection.Text = ";";
        }
    }
    

    【讨论】:

    • 它工作得很好,谢谢老兄。你到现在在哪里:D
    猜你喜欢
    • 1970-01-01
    • 2017-04-29
    • 1970-01-01
    • 2021-02-25
    • 2016-01-29
    • 2016-05-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多