在大文本文件中插入数字序列

时间:2013-08-12 18:40:13

标签: editor sequence text-editor large-files ultraedit

我需要以这种格式创建一个文件:

item0000001
item0000002
item0000003
item0000004
item0000005

我正在使用UltraEdit执行此操作,其中列模式包括插入编号(开始+增量,包括前导零)。

不幸的是,UltraEdit爆炸了超过100万行。

有没有人知道具有大文件容量且具有类似操作的文本编辑器?

1 个答案:

答案 0 :(得分:0)

BaltoStar尚未撰写使用过哪种版本的UltraEdit以及他是如何尝试创建该文件的。

但是,这是一个UltraEdit脚本,可用于创建一个文件,其中包含一个递增数字的行,根据最后一个数字,前导零。

要将该脚本与UltraEdit v14.20或UEStudio v9.00或更高版本一起使用,请复制脚本的代码块并将其粘贴到UltraEdit / UEStudio中具有DOS行终止的新ASCII文件中。将文件保存为例如 CreateLinesWithIncrementingNumber.js 到您首选的UE / UES脚本目录中。

现在通过单击菜单脚本中的菜单项运行活动脚本来运行脚本。

脚本会提示用户输入递增数字的第一个和最后一个值,以及递增数字左右两边的字符串,它们也可以是空字符串。

然后向后倾斜,看看脚本如何将带有递增数字的行写入块中的新文件中。我使用这个UltraEdit脚本在几秒钟内创建了一个超过150 MB的文件,其递增数字从0到5.000.000。

if (typeof(UltraEdit.clipboardContent) == "string")
{
   // Write in blocks of not more than 4 MB into the file. Do not increase
   // this value too much as during the script execution much more free
   // RAM in a continous block is necessary than the value used here for
   // joining the lines in user clipboard 9. A too large value results
   // in a memory exception during script execution and the user of the
   // script also does not see for several seconds what is going on.
   var nBlockSize = 4194304;

   // Create a new file and make sure it uses DOS/Windows line terminations
   // independent on the user configuration for line endings of new files.
   UltraEdit.newFile();
   UltraEdit.activeDocument.unixMacToDos();
   var sLineTerm = "\r\n";    // Type of line termination is DOS/Windows.

   // Ask user of script for the first value to write into the file.
   do
   {
      var nFirstNumber = UltraEdit.getValue("Please enter first value of incrementing number:",1);
      if (nFirstNumber < 0)
      {
         UltraEdit.messageBox("Sorry, but first value cannot be negative.");
      }
   }
   while (nFirstNumber < 0);

   // Ask user of script for the last value to write into the file.
   do
   {
      var nLastNumber = UltraEdit.getValue("Please enter last value of incrementing number:",1);
      if (nFirstNumber >= nLastNumber)
      {
         UltraEdit.messageBox("Sorry, but last value must be greater than "+nFirstNumber.toString(10)+".");
      }
   }
   while (nFirstNumber >= nLastNumber);

   var sBeforeNumber = UltraEdit.getString("Please enter string left of the incrementing number:",1);
   var sAfterNumber = UltraEdit.getString("Please enter string right of the incrementing number:",1);

   // http://stackoverflow.com/questions/16378849/ultraedit-how-do-i-pad-out-a-string-with-leading-blanks
   // Convert the highest number to a decimal string and get a copy
   // of this string with every character replaced by character '0'.
   // With last number being 39428 the created string is "00000".
   var sLeadingZeros = nLastNumber.toString(10).replace(/./g,"0");

   // Instead of writing the string with the incrementing number line
   // by line to file which would be very slow and which would create
   // lots of undo records, the lines are collected first in an array of
   // strings whereby the number of strings in the array is determined
   // by value of variable nBlockSize. The lines in the array are
   // concatenated into user clipboard 9 and written as block to the
   // file using paste command. That is much faster and produces just
   // a few undo records even on very large files.
   UltraEdit.selectClipboard(9);
   UltraEdit.clearClipboard();

   // Calculate number of lines per block which depends on the
   // lengths of the 4 strings which build a line in the file.
   var nLineLength = sBeforeNumber.length + sLeadingZeros.length +
                     sAfterNumber.length + sLineTerm.length;
   var nRemainder = nBlockSize % nLineLength;
   var nLinesPerBlock = (nBlockSize - nRemainder) / nLineLength;

   var asLines = [];
   var nCurrentNumber = nFirstNumber;

   while (nLastNumber >= nCurrentNumber)
   {
      // Convert integer number to decimal string.
      var sNumber = nCurrentNumber.toString(10);
      // Has the decimal string of the current number less
      // characters than the decimal string of the last number?
      if (sNumber.length < sLeadingZeros.length)
      {
         // Build decimal string new with X zeros from the alignment string
         // and concatenate this leading zero string with the number string.
         sNumber = sLeadingZeros.substr(0,sLeadingZeros.length-sNumber.length) + sNumber;
      }
      asLines.push(sBeforeNumber + sNumber + sAfterNumber);
      if (asLines.length >= nLinesPerBlock)
      {
         asLines.push(""); // Results in a line termination at block end.
         UltraEdit.clipboardContent = asLines.join(sLineTerm);
         UltraEdit.activeDocument.paste();
         UltraEdit.clearClipboard();
         asLines = [];
      }
      nCurrentNumber++;
   }
   // Output also the last block.
   if (asLines.length)
   {
      asLines.push("");
      UltraEdit.clipboardContent = asLines.join(sLineTerm);
      UltraEdit.activeDocument.paste();
      UltraEdit.clearClipboard();
   }
   // Reselect the system clipboard and move caret to top of new file.
   UltraEdit.selectClipboard(0);
   UltraEdit.activeDocument.top();
}
else if(UltraEdit.messageBox)
{
   UltraEdit.messageBox("Sorry, but you need a newer version of UltraEdit/UEStudio for this script.");
}
else
{
   UltraEdit.newFile();
   UltraEdit.activeDocument.write("Sorry, but you need a newer version of UltraEdit/UEStudio for this script.");
}
相关问题