JREPL.BAT v8.6 - regex text processor with support for text highlighting and alternate character sets

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Message
Author
zazaza
Posts: 1
Joined: 13 Sep 2022 05:08

Re: JREPL.BAT v8.6 - regex text processor with support for text highlighting and alternate character sets

#541 Post by zazaza » 13 Sep 2022 05:15

JREPL.BAT v8.6
bug - not working if \x00 in the replace string

working:

Code: Select all

call JREPL.BAT \x11\x22\x33 \x11\x00\x33 /XSEQ /M /F "test.bin" /O -
not working:

Code: Select all

set "F1=\x11\x22"
set "R1=\x11\x22"
set "F2=\x33\x44"
set "R2=\x33\x00\x44"
call JREPL.BAT "%F1% %F2%" "%R1% %R2%" /T " " /XSEQ /M /F "test.bin" /O -
not working:

Code: Select all

call JREPL find.txt replace.txt /T FILE /XSEQ /M /F "test.bin" /O -

find.txt
\x11\x22
\x33\x44

replace.txt
\x11\x22
\x33\x00\x44

Ron168
Posts: 1
Joined: 04 Jan 2023 09:01

Re: JREPL.BAT v8.6 - regex text processor with support for text highlighting and alternate character sets

#542 Post by Ron168 » 04 Jan 2023 09:16

Hi,

I've created a windows batch script to solve the issue below, however, it takes around 10 minutes for around 20k lines. This is too long.
This can be faster, that's when I found JREPL.

So, I'm new to using JREPL and I cannot figure this one out.

Some background information;
I'm using MYSQLDUMP to make a backup of a database's structure.

Unfortunately, the output is one big file.

The next, and missing, step would be to split this file into multiple files.

For this, I'm focusing on routines only.

This is what a part of the dump would look like:

Code: Select all

DELIMITER ;;
CREATE DEFINER=`rm_su`@`%` PROCEDURE `sp_name_1`()
BEGIN
	
	some code

END ;;
DELIMITER ;
/*!50003 SET sql_mode              = @saved_sql_mode */ ;
/*!50003 SET character_set_client  = @saved_cs_client */ ;
/*!50003 SET character_set_results = @saved_cs_results */ ;
/*!50003 SET collation_connection  = @saved_col_connection */ ;
/*!50003 DROP PROCEDURE IF EXISTS `sp_change_pl_for_project` */;
/*!50003 SET @saved_cs_client      = @@character_set_client */ ;
/*!50003 SET @saved_cs_results     = @@character_set_results */ ;
/*!50003 SET @saved_col_connection = @@collation_connection */ ;
/*!50003 SET character_set_client  = utf8mb4 */ ;
/*!50003 SET character_set_results = utf8mb4 */ ;
/*!50003 SET collation_connection  = utf8mb4_0900_ai_ci */ ;
/*!50003 SET @saved_sql_mode       = @@sql_mode */ ;
/*!50003 SET sql_mode              = 'NO_AUTO_VALUE_ON_ZERO' */ ;
DELIMITER ;;
CREATE DEFINER=`rm_su`@`%` PROCEDURE `sp_name_2`(
	some variables
)
BEGIN

some code

END ;;
DELIMITER ;
The output should be 2 separate files:
sp_name_1.sql

Code: Select all

DELIMITER ;;
CREATE DEFINER=`rm_su`@`%` PROCEDURE `sp_name_1`()
BEGIN
	
	some code

END ;;
DELIMITER ;
sp_name_2.sql

Code: Select all

DELIMITER ;;
CREATE DEFINER=`rm_su`@`%` PROCEDURE `sp_name_2`(
	some variables
)
BEGIN

some code

END ;;
DELIMITER ;
This extracts all data, including start (DELIMITER ;;) and end (DELIMITER ; ) strings into one file, just doesn't split them:

Code: Select all

jrepl "^[ \t]*DELIMITER ;;[\s\S]*?^\s*DELIMITER ;.*$" $0 /m /jmatch /f "dump.sql" /o output.sql
Something like this should work, I'm making a mistake somewhere, don't know what I'm doing wrong though.

Code: Select all

jrepl "^(DELIMITER ;;$[\s\S]*PROCEDURE `([a-zA-z]+)`[\s\S]*^DELIMITER ;)$" "openOutput($2.sql);$txt=$0" /m /jq /f "dump.sql"
Thanks in advance

Aacini
Expert
Posts: 1885
Joined: 06 Dec 2011 22:15
Location: México City, México
Contact:

Re: JREPL.BAT v8.6 - regex text processor with support for text highlighting and alternate character sets

#543 Post by Aacini » 04 Jan 2023 18:42

Ron168 wrote:
04 Jan 2023 09:16
Hi,

I've created a windows batch script to solve the issue below, however, it takes around 10 minutes for around 20k lines. This is too long.

. . . . .

Thanks in advance
Too long indeed... Are you using a lot of goto commands? File append >> instead of file redirection >?

Give the following Batch code a try:

Code: Select all

@echo off
setlocal EnableDelayedExpansion

set /A "last=0, n=0"
set "skip="
< dump.sql (
   for /F "delims=:" %%a in ('findstr /N /B "DELIMITER" dump.sql') do (
      if not defined skip (
         set /A "skip=%%a-last-1, last=%%a"
         for /L %%i in (1,1,!skip!) do set /P "="
      ) else (
         set /A "lines=%%a-last+1, last=%%a, n+=1"
         echo Creating file #!n!
         (for /L %%i in (1,1,!lines!) do (
            set "line="
            set /P "line="
            echo(!line!
         )) > sp_name_!n!.sql
         set "skip="
      )
   )
)
Antonio

Outbreaker
Posts: 10
Joined: 08 Aug 2023 15:16

Re: JREPL.BAT v8.6 - regex text processor with support for text highlighting and alternate character sets

#544 Post by Outbreaker » 20 Aug 2023 15:13

Hi,
Can JREPL.BAT auto detect if a text file is encoded in UTF-16?

Outbreaker
Posts: 10
Joined: 08 Aug 2023 15:16

How to replace only the second match in a text file?

#545 Post by Outbreaker » 29 Aug 2023 18:25

Hi,
Is it possible to tell "JREPL.bat" to only replace the second match "[AddReg]" in a text file :?:
I have tried all day long to find a solution for this, but failed. :(
The command /INC "120:390" doesn't help because the line numbers will change too much depending on the file.

Code: Select all

CALL ".\JREPL.bat" "[AddReg]" "[AddReg]\r\nNewLine." /XSEQ /L /F "test.txt" /O "test1.txt"

Code: Select all

[AddReg]
1TEST.
2TEST.
3TEST.
[AddReg]
1TEST.
2TEST.
3TEST.
[AddReg]
1TEST.
2TEST.
3TEST.

Aacini
Expert
Posts: 1885
Joined: 06 Dec 2011 22:15
Location: México City, México
Contact:

Re: How to replace only the second match in a text file?

#546 Post by Aacini » 29 Aug 2023 18:59

You don't need a sophisticated replacement tool like JREP.bat to perform a replacement as simple as this one. This Batch file do that:

Code: Select all

@echo off
setlocal EnableDelayedExpansion

rem Get the line number of *the second* appearance of "[AddReg]"
for /F "tokens=2 delims=:" %%a in ('findstr /N "[AddReg]" test.txt ^| findstr /N "^" ^| findstr "^2:"') do set "lines=%%a"

< test.txt (

   rem Copy up to that line
   for /L %%i in (1,1,%lines%) do (
      set /P "line="
      echo(!line!
   )

   rem Insert the desired data
   echo NewLine.

   rem Copy the rest
   findstr "^"

) > test1.txt
Output:

Code: Select all

[AddReg]
1TEST.
2TEST.
3TEST.
[AddReg]
NewLine.
1TEST.
2TEST.
3TEST.
[AddReg]
1TEST.
2TEST.
3TEST.
Antonio

Outbreaker
Posts: 10
Joined: 08 Aug 2023 15:16

Re: How to replace only the second match in a text file?

#547 Post by Outbreaker » 29 Aug 2023 19:11

@Aacini
I'm already using JREPL.BAT to edit over 500 lines of code. That's why it would be helpful too find a JREPL.BAT command too change only the second match in a text file.

Outbreaker
Posts: 10
Joined: 08 Aug 2023 15:16

Re: JREPL.BAT v8.6 - regex text processor with support for text highlighting and alternate character sets

#548 Post by Outbreaker » 06 Sep 2023 00:49

Hi,
I found a solution for the occurrence problem.
I think this can also be done with "/JENDLN" but had no luck in getting this to work.

Works:

Code: Select all

".\JREPL.bat" "\[AddReg\]" "++occ==2?$txt='[AddReg]\r\nTestLine.':$txt=$0" /M /JQ /JBEG "occ=0" /F "test.txt" /O "test1.txt"
Should also work but it doesn't:

Code: Select all

".\JREPL.bat" "\[AddReg\]" "$txt='[AddReg]\r\nTestLine.'" /JQ /JBEG "occ=0" /JENDLN "If (++occ==2) skip=true" /F "test.txt" /O "test1.txt"

Code: Select all

.\JREPL.bat" "\[AddReg\]" "$txt='[AddReg]\r\nTestLine.'" /JQ /JBEG "occ=0" /JENDLN "++occ; if (occ==2) {skip=false} else {skip=true}" /F "test.txt" /O "test1.txt"

Code: Select all

[AddReg]
1TEST.
2TEST.
3TEST.
[AddReg]
NewLine.
1TEST.
2TEST.
3TEST.
[AddReg]
1TEST.
2TEST.
3TEST.

dbenham
Expert
Posts: 2461
Joined: 12 Feb 2011 21:02
Location: United States (east coast)

Re: JREPL.BAT v8.6 - regex text processor with support for text highlighting and alternate character sets

#549 Post by dbenham » 06 Sep 2023 04:41

Good job.
You can simplify by using the predefined counter variable that is already initialized to 0

Code: Select all

jrepl "\[AddReg\]" "$txt=++counter==2?$0+'\r\nTestLine.':$0" /jq /f "test.txt" /o "test1.txt"
Dave Benham

Outbreaker
Posts: 10
Joined: 08 Aug 2023 15:16

Re: JREPL.BAT v8.6 - regex text processor with support for text highlighting and alternate character sets

#550 Post by Outbreaker » 09 Sep 2023 12:04

I like it simple. :)
But could this occurrence code trick not also be move to /JENDLN to make it even simpler?

Outbreaker
Posts: 10
Joined: 08 Aug 2023 15:16

Re: JREPL.BAT v8.6 - regex text processor with support for text highlighting and alternate character sets

#551 Post by Outbreaker » 21 Sep 2023 20:50

Here is a JREPL.BAT version that auto detect if a file is in UTF-16 by searching for a UTF16 HEX value in a file.
UTF-16LE with BOM = "FF FE" (At the begging of a file).
UTF-16LE without BOM = "0D 00 0A 00" (Windows HEX Line Break) / "0A 00" (Linux HEX Line Break).
The switch /UTFA will activate the UTF-16 auto detection.
https://send.cm/d/fVm1

JScript Part:

Code: Select all

// UTF-16 detection (/UTFA)
var objShell = new ActiveXObject("WScript.Shell")
filePath = "";
args = WScript.Arguments;
function_select = 0;
// file name as argument
for (var i = 0; i < args.length - 1; i++) {
  // debug WScript.Echo("Param: " +  args(i))
  if (args(i)=="JScriptFunc=1")
    function_select = 1;
  if (args(i)=="/F")
    filePath = args(i + 1).split("|");
}
// debug WScript.Echo("call--- function_select = " + function_select);
if (function_select==1) {
  filePath = filePath[0];
  if (filePath) {
    var fso = new ActiveXObject("Scripting.FileSystemObject");
    var found = 0;
    try {
      // Open the file for binary reading
      var file = fso.OpenTextFile(filePath, 1); // 1 indicates read mode,
      // Read the binary data
      var binaryData = file.ReadAll();
      // Close the file
      file.Close();
      if (binaryData.length > 1) {
        // If the first hex value in a file is "FF FE"
        for (var i = 0; i < 2; i++) {
          if (binaryData.charCodeAt(0) == 255 && binaryData.charCodeAt(i) == 254) {
            // debug WScript.Echo("Found start position.. ")
            found = 1;
          }
        }
      }
      if (binaryData.length > 2) {
        // If hex value in file matches "0D 00 0A 00"
        for (var i = 0; i <= binaryData.length - 2 && found == 0; i++) {
          // There should 0D 00 0A 00
          // 00 0D 00 0A 00 - can be only for English text
          if (binaryData.charCodeAt(i) == 13 &&
            binaryData.charCodeAt(i+1) == 0 ||
            binaryData.charCodeAt(i) == 10 &&
            binaryData.charCodeAt(i+1) == 0)
          {
            // debug WScript.Echo("Found position " + i)
            found = 1;
          }
        }
      }
      if (found == 1) {
        // debug WScript.Echo("/UTF=1 has been set");
        WScript.Quit(1);
      }
      else {
        WScript.Quit(0);
      }
    } catch (e) {
      // Handle any errors that occur during file operation
      WScript.Echo("An error occurred: " + e.message);
      WScript.Quit(0);
    }
  }
}

Post Reply