insert numerical sequence in large text file

1.1k Views Asked by At

I need to create a file in this format :

item0000001
item0000002
item0000003
item0000004
item0000005

I was doing this with UltraEdit which has column mode including insert number ( start + increment including leading zeros ).

Unfortunately, UltraEdit bombs out above 1 million rows.

Does anyone know of a text editor with large file capacity that has a similar operation?

1

There are 1 best solutions below

0
On

BaltoStar has not written which version of UltraEdit was used and how exactly he has tried to create the file.

However, here is an UltraEdit script which can be used to create a file with lines containing an incrementing number with leading zeros according to last number.

To use that script with UltraEdit v14.20 or UEStudio v9.00 or any later version, copy the code block of the script and paste it into a new ASCII file with DOS line terminations in UltraEdit/UEStudio. Save the file for example as CreateLinesWithIncrementingNumber.js into your preferred directory of UE/UES scripts.

Now run the script by clicking on menu item Run Active Script in menu Scripting.

The script prompts the user for first and last value of the incrementing number, and for strings left and right of the incrementing number which can be both also empty strings.

Then lean back and see how the script writes the lines with the incrementing number into a new file in blocks. I created a file with more than 150 MB with an incrementing number from 0 to 5.000.000 within a few seconds using this UltraEdit script.

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.");
}