0

如果我需要删除重复项

key = anything

不是

key=anything

关键也可以是任何东西

例如 edit_home=home必须到位

如果edit_home 是重复的,则必须删除edit_home = home 甚至其他字符串

对于文档的所有行

谢谢你

ps更清晰的例子:

one=you are
two=we are
three_why=8908908
one = good
two = fine
three_4 = best
three_why = win

从该列表中,我只需要保留:

one=you are
two=we are
three_why=8908908
three_4 = best // because three_4 doesn't have a duplicate

我找到了一种方法来做到这一点,但我需要正则表达式或插件或直接正则表达式(我不知道)提供更好的搜索列表支持。

那就是:我有两个文件要比较。

一个拥有完整的密钥,另一个拥有不完整的密钥。

我将第一个文件中的所有键与第二个文件中的所有键合并到一个新文件中,分组(因为键是分组的,例如许多键名为一个,许多键名为两个等等......)。然后我正则表达式替换新文件中的所有键

find (.*)(\s\=\s) replace with \1\=

所以他们都变成了key=anything

然后我用空替换 = 之后的所有内容以隔离键。

然后删除重复项。

在这一点上,我很难做类似的事情

^.*(^keyone\b|^keytwo\b|^keythree\b).*$

在我需要的文档中找到所有这些键。因此,我可以选择全部并用正确的键替换。

为什么?因为在这个例子中,键只有 3 个,但键确实很多,并且 find 字段在某个点中断。

怎么做才对?

更新:我找到了允许搜索许多字符串的 Toolbucket 插件,但另一个问题是除了重复之外,我还必须删除原始字符串。

也就是说,如果我找到 2 次相同的键“一”,我必须删除所有包含 1 的行。

4

3 回答 3

0

这是此任务的注释 UltraEdit 脚本。

// Note: This script does not work for large files as it loads the
// entire file content into very limited scripting memory for fast
// processing even with multiple GB of RAM installed.

if (UltraEdit.document.length > 0)  // Is any file opened?
{
   // Define environment for this script and select entire file content.
   UltraEdit.insertMode();
   UltraEdit.columnModeOff();
   UltraEdit.activeDocument.selectAll();

   // Determine line termination used currently in active file.
   var sLineTerm = "\r\n";
   if (typeof(UltraEdit.activeDocument.lineTerminator) == "number")
   {
      // The two lines below require UE v16.00 or UES v10.00 or later.
      if (UltraEdit.activeDocument.lineTerminator == 1) sLineTerm = "\n";
      else if (UltraEdit.activeDocument.lineTerminator == 2) sLineTerm = "\r";
   }
   else  // This version of UE/UES does not offer line terminator property.
   {
      if (UltraEdit.activeDocument.selection.indexOf(sLineTerm) < 0)
      {
         sLineTerm = "\n";          // Not DOS, perhaps UNIX.
         if (UltraEdit.activeDocument.selection.indexOf(sLineTerm) < 0)
         {
            sLineTerm = "\r";       // Also not UNIX, perhaps MAC.
            if (UltraEdit.activeDocument.selection.indexOf(sLineTerm) < 0)
            {
               sLineTerm = "\r\n";  // No line terminator, use DOS.
            }
         }
      }
   }

   // Get all lines of active file into an array of strings
   // with each string being one line from active file.
   var asLines = UltraEdit.activeDocument.selection.split(sLineTerm);
   var nTotalLines = asLines.length;

   // Process each line in the array.
   for(var nCurrentLine = 0; nCurrentLine < asLines.length; nCurrentLine++)
   {
      // Skip all lines not containing or starting with an equal sign.
      if (asLines[nCurrentLine].indexOf('=') < 1) continue;

      // Get string left to equal sign with tabs/spaces trimmed.
      var sKey = asLines[nCurrentLine].replace(/^[\t ]*([^\t =]+).*$/,"$1");

      // Skip lines beginning with just tabs/spaces left to equal sign.
      if (sKey.length == asLines[nCurrentLine].length) continue;
      var_dump(sKey);

      // Build the regular expression for the search in all other lines.
      var rRegSearch = new RegExp("^[\\t ]*"+sKey+"[\\t ]*=","g");

      // Ceck all remaining lines for a line also starting with
      // this key string case-sensitive with left to an equal sign.
      var nLineCompare = nCurrentLine + 1;
      while(nLineCompare < asLines.length)
      {
         // Does this line also has this key left to equal
         // sign with or without surrounding spaces/tabs?
         if (asLines[nLineCompare].search(rRegSearch) < 0)
         {
            nLineCompare++;   // No, continue on next line.
         }
         else  // Yes, remove this line from array.
         {
            asLines.splice(nLineCompare,1);
         }
      }
   }
   // Was any line removed from the array?
   if (nTotalLines == asLines.length)
   {
      UltraEdit.activeDocument.top();  // Cancel the selection.
      UltraEdit.messageBox("Nothing found to remove!");
   }
   else
   {
      // If version of UE/UES supports direct write to clipboard, use
      // user clipboard 9 to paste the lines into file with overwritting
      // everything as this is much faster than using write command in
      // older versions of UE/UES.
      if (typeof(UltraEdit.clipboardContent) == "string")
      {
         var nActiveClipboard = UltraEdit.clipboardIdx;
         UltraEdit.selectClipboard(9);
         UltraEdit.clipboardContent = asLines.join(sLineTerm);
         UltraEdit.activeDocument.paste();
         UltraEdit.clearClipboard();
         UltraEdit.selectClipboard(nActiveClipboard);
      }
      else UltraEdit.activeDocument.write(asLines.join(sLineTerm));

      var nRemoved = nTotalLines - asLines.length;
      UltraEdit.activeDocument.top();
      UltraEdit.messageBox("Removed " + nRemoved + " line" + ((nRemoved != 1) ? "s" : "") + " on updated file.");
   }
}

复制此代码并使用 UltraEdit 中的 DOS 行终止符将其粘贴到新的 ASCII 文件中。

接下来使用命令File - Save As将脚本文件(例如名称为RemoveDuplicateKeys.js )保存到%AppData%\IDMComp\UltraEdit\MyScripts或您想要保存 UltraEdit 脚本的任何位置。

打开Scripting - Scripts并将刚刚保存的 UltraEdit 脚本添加到脚本列表中。您也可以为此脚本输入描述。

打开带有列表的文件,或者如果该文件已经在 UltraEdit 中打开,则激活该文件。

通过在菜单Scripting中单击它来运行脚本,或者打开Views - Views/Lists - Script List并双击脚本。

于 2014-11-15T17:18:45.777 回答
0

Ctrl+F

查找选项卡

找到什么^.*\S=\S.*$

在当前文档中查找全部

将结果窗口中的结果复制到新窗口(第 1 行列表:第 2 行:第 3 行:...)

Ctrl+F

替换选项卡

(以下将删除每行的前导“行号:”)

找到什么^.*?\d:\s

替换为:空

于 2014-11-13T18:53:46.213 回答
0

好的,毕竟我写了这么多,一种解决方案可能是(因此,一旦我拥有合并的密钥)

(?m)^(.*)$(?=\r?\n^(?!\1).*(?s).*?\1)

有了这个我可以标记/突出显示所有重复的键:-)所以我可以只管理那些,从第一个列表中删除它们并将剩余的内容添加到第二个文件中......

如果有人有直接正则表达式的解决方案,将不胜感激

于 2014-11-14T03:57:29.737 回答