Replace line in text file : "sloppy" replacement vs JREPL

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
SIMMS7400
Posts: 546
Joined: 07 Jan 2016 07:47

Replace line in text file : "sloppy" replacement vs JREPL

#1 Post by SIMMS7400 » 06 Sep 2017 18:10

Hi Folks -

I need to replace the first line in a text file. Therefore, I've implemented this piece of code:

Code: Select all

SETLOCAL ENABLEEXTENSIONS DISABLEDELAYEDEXPANSION
   
SET FILE_SEARCH="Element","Desc","RM_IO","RM_IO_DESC","RM_Shortcode","PROTOCOL_NO","Source","RM_PFP","RM_PFP_Alias","RM_PFI","RM_PFI_Alias","RM_PFC","RM_PFC_Alias","PF_TherapeuticArea","ALLIANCE_PARTNER","DIRECT_INDIRECT","IP_OWNER"
SET FILE_REPLACE="Hierarchy ^(Code^)","Code Desc","MDM IO","MDM IO Desc","MDM IO Shortcode","MDM Protocal Number","MDM IO Source","PFP","PFP Alias","PFI","PFI Alias","PFC","PFC Alias","TA","ALLIANCE_PARTNER","DIRECT_INDIRECT","IP_OWNER"

SET "FILE_PATH=%LOCALEXPORTPATH%"
SET "FN=%TIGER_NAME%.csv"
SET "FILE=%FILE_PATH%%FN%"
   
    FOR /f "delims=" %%L IN ('TYPE "%FILE%" ^& BREAK ^> "%FILE%" ') DO (
        SET "LINE=%%L"
        SETLOCAL ENABLEDELAYEDEXPANSION
        SET "LINE=!LINE:%FILE_SEARCH%=%FILE_REPLACE%!"
        >>"%FILE%" ECHO !LINE!
        ENDLOCAL
    )


However, I'm exploring the JREPL solution which would be much quicker but I'm having difficulty getting it to work. Where am I going wrong? Thanks!

Code: Select all

@ECHO OFF 

CALL _env.cmd

SETLOCAL ENABLEEXTENSIONS DISABLEDELAYEDEXPANSION
   
SET NEWLINE="Hierarchy ^(Code^)","Code Desc","MDM IO","MDM IO Desc","MDM IO Shortcode","MDM Protocal Number","MDM IO Source","PFP","PFP Alias","PFI","PFI Alias","PFC","PFC Alias","TA","ALLIANCE_PARTNER","DIRECT_INDIRECT","IP_OWNER"

SET "FILE_PATH=%LOCALEXPORTPATH%"
SET "FN=SAP_to_Order_Reference_Mapping.csv"
SET "FILE=%FILE_PATH%%FN%"
   
jrepl "^FMOD.*" "%NEWLINE%" /jbegln "skip=(ln!=1)" /f "%FILE%" /o -

pause

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

Re: Replace line in text file : "sloppy" replacement vs JREPL

#2 Post by dbenham » 06 Sep 2017 22:45

I immediately see four problems with your JREPL code:

1) You cannot include quote literals within arguments to JREPL (or any other CSCRIPT program)

2) Your quoted replacement argument has a mixture of internal quotes, commas, and spaces, resulting in the appearance of more than one argument

3) Your find argument is totally different than your working pure batch code. But you are already restricting the search to the first line, so you could simply use "^.*"

4) You did not use CALL JREPL, so your script transfers control to JREPL, and PAUSE never executes.

But you don't really need JREPL to get a fast solution. Assuming your file does not have as many as 64k lines, and it doesn't have any tab characters, then you could simply do the following: (I've eliminated most of your environment variables to focus on the important bit)
- Write the new first line to a new file
- Use MORE to append the original file, skipping the first line
- MOVE the new file to replace the original

Code: Select all

@echo off
setlocal
set "file=yourFile.csv"
>"%file%.new" echo "Hierarchy ^(Code^)","Code Desc","MDM IO","MDM IO Desc","MDM IO Shortcode","MDM Protocal Number","MDM IO Source","PFP","PFP Alias","PFI","PFI Alias","PFC","PFC Alias","TA","ALLIANCE_PARTNER","DIRECT_INDIRECT","IP_OWNER"
>>"%file%.new" more +1 "%file%"
move /y "%file%.new" "%file%" >nul

But if your file contains tabs, or if it exceeds 64k lines, then you might still want to use JREPL.

You can use the /INC option to only search and replace the first line. And you can use the /V option to read your search and replace strings from variables, so you don't have to worry about quotes in the string.

Code: Select all

@echo off
setlocal
set "find=^.*"
set repl="Hierarchy (Code)","Code Desc","MDM IO","MDM IO Desc","MDM IO Shortcode","MDM Protocal Number","MDM IO Source","PFP","PFP Alias","PFI","PFI Alias","PFC","PFC Alias","TA","ALLIANCE_PARTNER","DIRECT_INDIRECT","IP_OWNER"
call jrepl find repl /v /inc 1 /f "yourFile.csv" /o -
pause


Dave Benham

SIMMS7400
Posts: 546
Joined: 07 Jan 2016 07:47

Re: Replace line in text file : "sloppy" replacement vs JREPL

#3 Post by SIMMS7400 » 07 Sep 2017 05:07

Dave -

Thank you very much. I will use your JREPL solution has my file has over 64 lines.

Have a great day!

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

Re: Replace line in text file : "sloppy" replacement vs JREPL

#4 Post by dbenham » 07 Sep 2017 05:20

64k = 64 thousand :)

SIMMS7400
Posts: 546
Joined: 07 Jan 2016 07:47

Re: Replace line in text file : "sloppy" replacement vs JREPL

#5 Post by SIMMS7400 » 07 Sep 2017 05:30

Yup - 64k lines!

:mrgreen:

Post Reply