Question

Not sure if this is possible even with regex/Sublime Text 2. Looking to take the first (positive) value (1759) of a line and copy it to the end of each related line in comment format. Copy is made to all (negative vale "B" lines until the next/new positive line, where that positive value (1753) is copied to the related lines. I have over 1600 lines with this format to comment on, wondering if there was a quicker method than entering each one manually. Note "B" lines are not related to "C" lines.

s(1759,"B 1",{99168,99157,99158,99159,105779,105792},4) 
s(-1764,"B 2 (L)",{105819,105140,104956,105141,105086,105119},nil) 
s(-1763,"B 3 (R)",{99086,99080,99081,99082,105086,105119},4) 
s(-1762,"B 4 (L)",{105432,105638,105454,105639,105584,105617},nil) 
s(-1761,"B 5 (R)",{99406,99402,99403,99404,105584,105617},4) 
s(-1760,"B 6 (L)",{105183,105809,105754,105800,105779,105792},nil)
s(1753,"C",{105161,99108,99109,99104,105784,105790},64)
s(-1755,"C (R)",{105410,99352,99353,99354,105597,105598},64)
s(-1754,"C (L)",{105329,105161,105259,105404,105755,105784,105790},nil)

into:

s(1759,"B 1",{99168,99157,99158,99159,105779,105792},4) --1759 
s(-1764,"B 2 (L)",{105819,105140,104956,105141,105086,105119},) --1759 
s(-1763,"B 3 (R)",{99086,99080,99081,99082,105086,105119},4) --1759 
s(-1762,"B 4 (L)",{105432,105638,105454,105639,105584,105617},) --1759 
s(-1761,"B 5 (R)",{99406,99402,99403,99404,105584,105617},4) --1759 
s(-1760,"B 6 (L)",{105183,105809,105754,105800,105779,105792},) --1759 
s(1753,"C",{105161,99108,99109,99104,105784,105790},64) -- 1753
s(-1755,"C (R)",{105410,99352,99353,99354,105597,105598},64) -- 1753
s(-1754,"C (L)",{105329,105161,105259,105404,105755,105784,105790},nil) -- 1753
Was it helpful?

Solution

This is the closest I could get to what you want to do using ST.
Find What: s\((?<!\-)(\d+)(.+\n)(?:((?:s\((?=\-).+\d+\n)+)|(s\((?=\-).+))+
Replace With: s($1$2$3$4 --$1

You will have to run it multiple times though, as it only does one at a time.

OTHER TIPS

I think, this task cannot be done using just regular expression replaces.

I'm not using Sublime text editor and therefore cannot offer a solution which you can use with this text editor.

However, perhaps there are other people having a similar task and therefore offer a scripting solution for text editor UltraEdit.

The following UltraEdit script reformats your input data to the output data.

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

   // Check if last line of file has a line termination and if this
   // is not the case, append a line termination to end of the file.
   UltraEdit.activeDocument.bottom();
   if (UltraEdit.activeDocument.isColNumGt(1))
   {
      UltraEdit.activeDocument.insertLine();
   }
   // Move caret to top of the active file.
   UltraEdit.activeDocument.top();

   // Select Perl regular expression engine.
   UltraEdit.perlReOn();

   // Define once all find and replace parameters
   // not changed during execution of the script.
   UltraEdit.activeDocument.findReplace.matchCase=true;
   UltraEdit.activeDocument.findReplace.matchWord=false;
   UltraEdit.activeDocument.findReplace.regExp=true;
   UltraEdit.activeDocument.findReplace.searchDown=true;
   if (typeof(UltraEdit.activeDocument.findReplace.searchInColumn) == "boolean")
   {
      UltraEdit.activeDocument.findReplace.searchInColumn=false;
   }
   UltraEdit.activeDocument.findReplace.preserveCase=false;
   UltraEdit.activeDocument.findReplace.replaceAll=true;
   UltraEdit.activeDocument.findReplace.replaceInAllOpen=false;

   UltraEdit.activeDocument.findReplace.mode=0;   // Find in current file.

   // Run the following loop until no line is found anymore up to
   // end of the file starting with "s(" and a positive number.
   while(UltraEdit.activeDocument.findReplace.find("^s\\(\\d+"))
   {
      // Copy just the found number into a string variable
      var sNumber = UltraEdit.activeDocument.selection.substr(2);
      // Remember the number of the current line.
      var nBlockBegin = UltraEdit.activeDocument.currentLineNum;
      // Run the same find as before to find next line starting with "s("
      // and a positive number to determine the lines of the block to modify.
      if(!UltraEdit.activeDocument.findReplace.find("^s\\(\\d+"))
      {
         UltraEdit.activeDocument.bottom();
      }
      var nNextBlock = UltraEdit.activeDocument.currentLineNum;

      // Select from current position of caret to beginning of the block.
      UltraEdit.activeDocument.gotoLineSelect(nBlockBegin,1);
      UltraEdit.activeDocument.endSelect();

      // The following Replace All is done only on selected lines.
      UltraEdit.activeDocument.findReplace.mode=1;

      // On all selected lines ending with "nil)" or just with ")" and
      // optional spaces/tabs remove "nil" and append " --" and the number.
      UltraEdit.activeDocument.findReplace.replace("(?:nil)?\\)[ \\t]*$", ") --"+sNumber);

      // Switch back to a find in current file.
      UltraEdit.activeDocument.findReplace.mode=0;

      // Move caret to beginning of next block or end of file.
      UltraEdit.activeDocument.gotoLine(nNextBlock,1);
   }
   UltraEdit.activeDocument.top();
}

I tested this script on input data example with UltraEdit v21.10.0.1027.

PS: Your output data is a little bit inconsistent. On the lines ending with 1759 there is no space between -- and the number, whereas on the lines ending with 1753 there is a space after --. And on 3 lines nil is removed, but not on last line of output data example.

Make file1.txt from your source file (insert all your 1600 lines):

file1.txt

local comment, group
print((([[
s(1759,"B 1",{99168,99157,99158,99159,105779,105792},4)
s(-1764,"B 2 (L)",{105819,105140,104956,105141,105086,105119},nil)
s(-1763,"B 3 (R)",{99086,99080,99081,99082,105086,105119},4)
s(-1762,"B 4 (L)",{105432,105638,105454,105639,105584,105617},nil)
s(-1761,"B 5 (R)",{99406,99402,99403,99404,105584,105617},4)
s(-1760,"B 6 (L)",{105183,105809,105754,105800,105779,105792},nil)
s(1753,"C",{105161,99108,99109,99104,105784,105790},64)
s(-1755,"C (R)",{105410,99352,99353,99354,105597,105598},64)
s(-1754,"C (L)",{105329,105161,105259,105404,105755,105784,105790},nil)
]]):gsub('(s%((.-),"(%w+).-)\n',
   function(line, next_comment, next_group)
      if group ~= next_group then
         group = next_group
         comment = next_comment
      end
      return line..' -- '..comment..'\n'
   end
)))

and execute it in Lua

lua file1.txt > file2.txt

After that file2.txt will contain modified code

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top