Viewing 7 posts - 1 through 7 (of 7 total)
  • Author
    Posts
  • #21408
    David
    Participant

    Hello,
    I have a file which is like the following:

    partnr=[1!#10]
    productgroup=[1!#2]
    productsubgroup=[1!#3]
    description1="zh_CN@"+[1!#15]+";"+"en_US@"+[1!#16]+";"
    note="zh_CN@"+[1!#17]+";"+"en_US@"+[1!#18]+";"
    assemblyposition count=[1!#4]
    parentvariant=[1!#5]
    partnr=[1!#6]
    pos=[1!#7]

    I often edit this file. Sometimes it includes more than 100 rows. The boring thing is the string after equal sign(=). I have to modify them one by one to make them be consecutive. They should be:
    =[1!#1]
    =[1!#2]
    =[1!#3]
    ….
    =[1!#132]
    =[1!#133]

    I want to use a macro to do that automatically. If the row includes string like “[1!#\d]”, then I will replace the number in this string with progressive changed number.
    My macro is like the following, but it doesn’t work as I want. May I trouble you help me? Thanks!

    redraw = false;
    document.selection.SelectAll();
    var count=1;
    var strFind="\[1!#(.+?)\]";
    
    while(document.selection.Find("\[1!#", eeFindNext))
    {
    	strResult="\[1!#"+count+"\]";
    	document.selection.Replace(strFind,strResult, eeReplaceSelOnly | eeFindReplaceRegExp);
     	count++;
    }

    My result is like this:

    partnr=[[1!#1]!#10]
    productgroup=[[1!#2]!#2]
    productsubgroup=[[1!#3]!#3]
    description1="zh_CN@"+[[1!#4]!#15]+";"+"en_US@"+[[1!#5]!#16]+";"
    #21409
    Stefan
    Participant

    the string after equal sign(=). I have to modify them one by one to make them be consecutive.

    FROM:
    partnr=[1!#10]
    productgroup=[1!#2]
    productsubgroup=[1!#3]
    description1=”zh_CN@”+[1!#15]+”;”+”en_US@”+[1!#16]+”;”
    note=”zh_CN@”+[1!#17]+”;”+”en_US@”+[1!#18]+”;”
    assemblyposition count=[1!#4]
    parentvariant=[1!#5]
    partnr=[1!#6]
    pos=[1!#7]
      
      
    TO:
    partnr=[1!#1]
    productgroup=[1!#2]
    productsubgroup=[1!#3]
    partnr=[1!#4]
    productgroup=[1!#5]
    productsubgroup=[1!#6]
    description1=”zh_CN@”+[1!#15]+”;”+”en_US@”+[1!#16]+”;”
    note=”zh_CN@”+[1!#17]+”;”+”en_US@”+[1!#18]+”;”
    assemblyposition count=[1!#7]
    parentvariant=[1!#8]
    partnr=[1!#9]
    pos=[1!#10]
      
      
    Use for example:

    if( document.selection.IsEmpty ) {document.selection.SelectAll();}
    sSel = document.selection.text; //alert(sSel);
    
    aLines = sSel.split("\r\n");
    
    aOut="";
    iCount=1;
    
    for (var i=0,x=aLines.length;i<x;i++) {
    	if (aLines[i].match(/=\[1!#\d+\]/)){
    		aOut += aLines[i].replace(/=\[1!#\d+\]/,'=[1!#'+iCount+']') + "\r\n";
    		iCount++;
    	}
    	else {
    		aOut += aLines[i] + "\r\n";
    	}
    };
    
    alert(aOut);
    //document.selection.text = aOut;

     
      
      
      
    HTH? ;-)

    #21410
    David
    Participant

    Thank you, Stefan! You’re so warm~~
    My description is not so clear, sorry for that! What I want is:
    FROM:

    partnr=[1!#10]
    productgroup=[1!#2]
    productsubgroup=[1!#3]
    description1=”zh_CN@”+[1!#15]+”;”+”en_US@”+[1!#16]+”;”
    note=”zh_CN@”+[1!#17]+”;”+”en_US@”+[1!#18]+”;”
    assemblyposition count=[1!#4]
    parentvariant=[1!#5]
    partnr=[1!#6]
    pos=[1!#7]

    TO:

    partnr=[1!#1]
    productgroup=[1!#2]
    productsubgroup=[1!#3]
    partnr=[1!#4]
    productgroup=[1!#5]
    productsubgroup=[1!#6]
    description1=”zh_CN@”+[1!#7]+”;”+”en_US@”+[1!#8]+”;”
    note=”zh_CN@”+[1!#9]+”;”+”en_US@”+[1!#10]+”;”
    assemblyposition count=[1!#11]
    parentvariant=[1!#12]
    partnr=[1!#13]
    pos=[1!#14]

    Use the function “split” “match” and “replace” of Java script is a good way. If possible to use the function “replace” of Emeditor syntax? If yes, it looks a little simple~~~

    #21411
    Stefan
    Participant

    Sorry, but with “document.selection.Replace” I get a mess like you have experienced, even replace ‘a’ by ‘X’ didn’t work for the whole example text.
     

    nLines = document.GetLines();
    var count=1;
    for(L=1;L<nLines;L++){
    	document.selection.SelectLine();
    	//document.selection.Replace("\[1!#\d+\]","[1!#"+count+"]", eeReplaceSelOnly | eeReplaceAll | eeFindReplaceRegExp);
    	document.selection.Replace("a","X", eeReplaceSelOnly | eeReplaceAll );
     	count++;
     	document.selection.LineDown();
    }

     
     
    Highly probable there is a trick I don’t get. Oh, and I use 15.7.0./ 32-bit portable here.
     
     
     

    #21412
    LTT
    Participant

    If my understanding is correct, in such a case, you don’t have to use a macro, or write it that way.
    Just try these nimble steps:
    (Multiple Selections should be enabled. I think you know what it is.)

    Select “[1!#”;
    Select All Occurrences (in context menu. Default shortcut key: CTRL+Shift+A);
    (If the first string is complex, you can input a regular expression and use “Select All” button in Find dialog box;)
    Press Esc key once (and only once) to collapse the selections;
    (Now all the cursors should be blinking just before the numbers you are going to replace;)
    Word Right Extend (Default: Ctrl+Shift+Right);
    (Now all the target numbers should be selected;)
    Numbering (in context menu: Advanced. Default: Alt+N);
    In Numbering dialog box:
    ^ First Line: 1
    ^ Increment: 1
    ^ Decimal
    Done.

    You can record this procedure as a macro.
    But I’d suggest keeping these smart commands in mind and using them deftly in different cases. I think such operations are more flexible, more interesting, and more “EmEditorism”.

    However, if you’d rather use a macro of your style:

    redraw = false;
    document.selection.StartOfDocument();
    var count = 1;
    var strFind = "\\[1!#";
    while(document.selection.Replace(strFind + "\\d+", strFind + count, eeFindReplaceRegExp | eeFindNext)) count++;
    #21413
    David
    Participant

    Hello,Stefan! Thank you again!
    Hello,LTT. Your macro works very well. Thank you too!

    #21420
    Stefan
    Participant

    Thanks LTT, both of your solutions works! Well done. ( Double JS-and-RegEx escaping tricked me again ;-) )

Viewing 7 posts - 1 through 7 (of 7 total)
  • You must be logged in to reply to this topic.