Search code examples
jrepl

jrepl - how to print out only first modified line


I have several lines text file and would like to get only the first line that is modified.

For example I have the follow text file and would like to get the ID number:

something 1 is written here
My ID 1234 for file A 
something 2 is written here
My ID 1234 for file A 
something 3 is written here

My batch file jrepl command is:

call jrepl ".*My ID ([0-9]+).*" "$1" /F "file.txt" /A /O "result.txt"

But the results is for all modified lines, thus "results.txt" contains :

1234
1234

How can I command jrepl to stop after the first modified line that is printed out ?

I must say that I saw a similar question here, but I didn't succeed to follow , also seems to me complicated for such simple need.


Solution

  • Use JScript for the replacement argument so you can set jrepl's global variable 'skip' to true after the first match. Now, while skip variable is true, the next lines are skipped without being printed out.

    call ..\jrepl\jrepl ".*My ID ([0-9]+).*"    "$txt = $1;skip=true;" /JMATCHQ  /F "file1.txt" /A /O "result.txt"
    

    Description:

    /JMATCHQ - defines that replacement argument is a JScript. In such case, the replace value must be set into $txt.

    Thus, when the first match happens, the JScript line is called and does two things:

    1. set replace value to $txt - this will be printed out.
    2. set global jrepl JS variable 'skip' to true - thus, next lines matched won't be printed out.