Hi,
I have a case a bit complicated. I would like to replace a list of strings in some files with another list from a file. It is like an update. In fact, strings are syntax variables for Sublime Text.
TEMP\all_quests.txt (containing strings only, one per line)
Code: Select all
new_quest1
new_quest2
new_quest3
new_quest4
Part of Game_Syntaxes\syntax_file_1.sublime-syntax
Code: Select all
...
VARIABLE_ALL_QUESTS: |-
(?x:
old_quest1
| old_quest2
| old_quest3
| old_quest4
| old_quest5
| old_quest6
)
...
What I want:
Same part of Game_Syntaxes\syntax_file_1.sublime-syntax
Code: Select all
...
VARIABLE_ALL_QUESTS: |-
(?x:
new_quest1
| new_quest2
| new_quest3
| new_quest4
)
...
As you can see, the syntax is not the same in TEMP and in Game_Syntaxes (in Game_Syntaxes there is an indentation, the first string has no pipe and the number of strings is not always the same). The first line (
VARIABLE_ALL_QUESTS: |-) can exist zero or one time per file. If it exists, the list must be updated. The syntax in Game_Syntaxes is always the same (only strings and VARIABLE_ALL_QUESTS change).
All strings match the regex [A-Za-z0-9_-]+ (no spaces, no accents, etc.) and are not very long (<50 characters), but can be thousands.
There are
- 52 lists (one list per file in TEMP)
- 64 syntax files in Game_Syntaxes (some of them have 30 000 lines, but < 800kb).
Currently I use a script working fine, but it is very slow. I'm pretty sure it can be a lot faster. Furthermore, I use a workaround (put the list in one line) because I have no ideas how replacing several lines by several lines from a file. Avoiding it should speed things up.
Code: Select all
:::: This first part exists 52 times... Not fast either
:: remove duplicated strings
call BATCH_JREPL "" "" /N 10 /f "TEMP\ztemp0_all_quests.txt" ^
| sort /REC 30000 /+11 ^
| BATCH_JREPL ".*?:(.*)$" "x=p;p=$1;$txt=($1==x?false:$src);" /jmatchq /jbeg "var p='',x" ^
| sort /REC 30000 ^
| BATCH_JREPL "^.*?:" "" > "TEMP\ztemp1_all_quests.txt"
:: sort the lines
sort /REC 30000 /c TEMP\ztemp1_all_quests.txt /o TEMP\all_quests.txt
:: change the syntax
call BATCH_JREPL "(\r\n)" "|" /i /m /x /f TEMP\all_quests.txt /o -
call BATCH_JREPL "\|$" "" /i /m /x /f TEMP\all_quests.txt /o -
call BATCH_JREPL "^" "VARIABLE_ALL_QUESTS: '(" /i /m /x /f TEMP\all_quests.txt /o -
call BATCH_JREPL "$" ")'" /i /m /x /f TEMP\all_quests.txt /o -
:::: Workaround 1 - 30 seconds
:: In one line
for %%F in ("Game_Syntaxes\*.sublime-syntax") do (
call BATCH_JREPL "(^ # MODDABLE\r?\n)([\s\S]*?)(?=\n # NON MODDABLE$)" "$txt=$1+$2.replace(/\r\n \| /gm,'|')" /jq /m /f "%%F" /o -
call BATCH_JREPL "(^ # MODDABLE\r?\n)([\s\S]*?)(?=\n # NON MODDABLE$)" "$txt=$1+$2.replace(/\r\n \)/gm,')\'')" /jq /m /f "%%F" /o -
call BATCH_JREPL "(^ # MODDABLE\r?\n)([\s\S]*?)(?=\n # NON MODDABLE$)" "$txt=$1+$2.replace(/\|-\r\n \(\?x:\r\n\s*/gm,'\'(')" /jq /m /f "%%F" /o -
)
:::: In fact, there are 52 lines like this one. >6 minutes (Just for information, I tried with for %%F and it is even longer)
for /f "delims=" %%a in ('dir /b /a-d "Game_Syntaxes\*.sublime-syntax" ') do (
call BATCH_JREPL "VARIABLE_ALL_QUESTS: .*$" "$txt=stdin.AtEndOfStream?$0:stdin.ReadLine()" /jq /f "Game_Syntaxes\%%~a" <"TEMP\all_quests.txt" /o -
)
:::: Workaround 2 - 50 seconds
:: In several lines
for /f "delims=" %%a in ('dir /b /a-d "Game_Syntaxes\*.sublime-syntax" ') do (
call BATCH_JREPL "'\(" "(?x:\r\n " /inc "/^ # MODDABLE$/+1:/^ # NON MODDABLE$/-1" /x /f "Game_Syntaxes\%%~a" /o -
call BATCH_JREPL "\|" "\r\n | " /inc "/^ # MODDABLE$/+1:/^ # NON MODDABLE$/-1" /x /f "Game_Syntaxes\%%~a" /o -
call BATCH_JREPL "\)'" "\r\n )" /inc "/^ # MODDABLE$/+1:/^ # NON MODDABLE$/-1" /x /f "Game_Syntaxes\%%~a" /o -
call BATCH_JREPL "\: " ": |-\r\n " /inc "/^ # MODDABLE$/+1:/^ # NON MODDABLE$/-1" /x /f "Game_Syntaxes\%%~a" /o -
)
I tried to be clear. If you accept to help me on this case and needs to have all real files, I can send them to you by pm.
Thank you