-
Notifications
You must be signed in to change notification settings - Fork 3
/
jrepl.bat
2631 lines (2572 loc) · 117 KB
/
jrepl.bat
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
@if (@X)==(@Y) @end /* Harmless hybrid line that begins a JScript comment
@goto :Batch
::JREPL.BAT by Dave Benham
:: https://www.dostips.com/forum/viewtopic.php?t=6044
::
:: echo "<https://www.google.com>" | jrepl.bat "<(https://www.google.com)>" "[abc]$1[/abc]"
:: "[abc]https://www.google.com[/abc]"
::
::/History
::
:: 2020-07-30 v8.6: Added options to /K, /R, and /MATCH to count the number
:: of matches or rejects instead of printing them.
:: Added the counter /JScript variable.
:: Fixed /T Pig Latin example - $0 corrected to read $&
:: 2020-02-29 v8.5: Added /EOL option to set the end of line terminator.
:: Added the eol global jscript variable.
:: Doc fix - No EOL if /RTN option specifies a :LineNumber.
:: 2019-09-13 v8.4: Bug fix - /K,/R did not work with /EXC,/INC as of v8.0
:: 2019-07-16 v8.3: Documentation correction - Binary data with null bytes may
:: be read via ADO.
:: 2019-06-05 v8.2: /RTN bug fix - preserve Unicode by using CHCP 65001/utf-8
:: to transfer value to variable unless /XFILE used.
:: 2019-05-19 v8.1: Add /VT to enable Virtual Terminal ANSI escape sequences.
:: Code courtesy of DosTips user aGerman (Steffen).
:: 2019-05-15 v8.0: Add /Hxxx options for highlighting matched/replaced text.
:: Bug fix - /OFF was wrong when used with /P.
:: Change - /OFF is now incompatible with /PREPL.
:: Change - /PREPL may now be used with /K and /R.
:: Doc fix - corrected syntax for /F [|NB] option.
:: Doc fix - /OFF is for first match if used with /K.
:: 2018-10-20 v7.15: Add a string literal syntax to the /INC and /EXC options.
:: 2018-10-15 v7.14: Bug fix - User defined variables declared in /JBEG named
:: str and/or obj were getting clobbered.
:: Bug fix - Internal variable xbytes was visibile to user
:: supplied JScript.
:: 2018-07-19 v7.13: Bug fix - /INC and /EXC regex failed to match any
:: line that immediately followed a prior block.
:: Added ADO code to create XBYTES.DAT in case CERTUTIL is
:: missing. Code courtesy of DosTips user aGerman (Steffen).
:: 2018-07-18 v7.12: Fixed XBYTES.DAT creation cleanup bugs, and improved docs
:: 2018-03-26 v7.11: Add support for /O "-|UTF-?|NB" (overwrite without BOM)
:: 2018-03-14 v7.10: Now can block BOM in ADO output files by appending |NB
:: to |CharSet in the /O option and OpenOutput() function.
:: 2017-11-23 v7.9: Allow escape sequences with /T "" coupled with /XSEQ
:: Added /PREPL option to augment /P behavior
:: Bug fix - Force /L when /T "" used, as per documentation
:: Bug fix - Allow /?charset/search to include non alpha
:: 2017-11-13 v7.8: Added \x{nn-mm} and \x{nn-mm,CharSet} escape sequences
:: Split /X into /XFILE and /XSEQ - /X implies both
:: Add :FILE syntax for /K and /R to load searches from file
:: Fixed /XSEQ escaped backslash bug with /INC, /EXC, AND /P
:: 2017-10-24 v7.7: Fixed broken Microsoft documentation links
:: Allow /O "-|CharSet"
:: Fixed decode(Str[,CharSet]) bug when CharSet is undefined
:: 2017-10-08 v7.6: Fixed /?Intro syntax help for /?Charset/[Query]
:: 2017-10-08 v7.5: Added /?CHARSET and /?XREGEXP web page help options
:: Added /?CHARSET/[query] List character sets help option
:: Fixed ADO output.WriteLine() to use \r\n instead of \n
:: Improved documentation: /EXC, /OFF, /U, /?HELP, decode()
:: 2017-09-25 v7.4: Modified /X \xnn extended ASCII escape sequence to support
:: any single byte character set.
:: Added /X \x{nn,Charset} escape sequence.
:: Added /XBYTES and /XBYTESOFF options.
:: Modified decode() to support the new /X \xnn behavior.
:: 2017-09-23 v7.3: Fixed /O - support for ADO input.
:: 2017-09-23 v7.2: Improved documentation of new 7.0 features.
:: Bug fix - /T FILE ADO support was broken
:: 2017-09-08 v7.1: Bug fix - v7.0 failed if Find or Replace contained )
:: 2017-09-08 v7.0: Added /XREG and /TFLAG for XRegExp regex support.
:: Added /UTF for UTF-16LE support.
:: Added /X support for the \u{N} unicode escape sequence.
:: Added |CharSet syntax for file names to allow reading
:: and writing via ADO with a specified character set.
:: Exposed the fso FileSystemObject to user JScript.
:: Augmented openOutput for Unicode and ADO support.
:: 2017-08-25 v6.8: Added /X support for the \c caret escape sequence.
:: Added /APP - append to the output file.
:: Added the openOutput(file[,appendBoolean]) function.
:: 2017-04-09 v6.7: Corrected /OFF /EXC & /INC documentation + spelling fixes.
:: 2016-12-23 v6.6: Help correction: Fixed return codes in /?RETURN section.
:: 2016-11-13 v6.5: Modify /X to consistently preserve extended ASCII.
:: New option /RTN writes result to a variable.
:: 2016-11-01 v6.4: Bug fix - v6.3 had inverted /EXC result.
:: 2016-10-13 v6.3: Improved performance by dynamically generating main loop
:: code based on chosen options.
:: 2016-10-13 v6.2: Bug fix - /J, /JQ, /JMATCH, /JMATCHQ did not work with /P.
:: 2016-10-08 v6.1: Bug fix - v6.0 broke /JBEG and /JLIB, all fixed.
:: 2016-10-08 v6.0: Added /K - search and write matching lines.
:: Added /R - search and write non-matching lines.
:: Added /MATCH - search and write each match on a new line.
:: Added /P - Pre-filter regex before normal search/replace.
:: Added /PFLAG - set search flags for /P regex
:: Added /JQ and /JMATCHQ as Quick forms of /J and /JMATCH.
:: Augmented /INC and /EXC so can now specify lines by regex.
:: Changed behavior - /V now applies to /INC and /EXC.
:: Improved performance of /INC, /EXC, /T, /JBEGLN, /JENDLN.
:: Added HISTORY and UPDATE topics to the help system.
:: 2016-09-27 v5.2: Bug fix - Search & Replace now ignore /V if /T FILE used.
:: Added a /T FILE example to the documentation.
:: 2016-09-20 v5.1: Added the FILE alternative for the /T option.
:: 2016-09-18 v5.0: Added the /U option for Unix line terminators of /n.
:: 2016-08-04 v4.6: Fixed the /N documentation (repaired missing line)
:: 2016-08-03 v4.5: Added /D option to specify delimiter for /N and /OFF.
:: 2016-08-02 v4.4: Bug fix - /C count was wrong when last line did not end
:: with new line. This also affected /INC and /EXC.
:: 2016-07-30 v4.3: Added rpad() function and improved lpad()
:: 2016-06-24 v4.2: Improved the /?Options help.
:: 2016-06-23 v4.1: Added /T option examples to the help.
:: Added ability to request help on a single option or topic.
:: 2016-06-19 v4.0: Added the /INC and /EXC options.
:: 2016-03-27 v3.8: Bug fix - Hide leaked global variables i, lib, libs, rtn2.
:: Bug fix - Work around %~f0 bug when command is quoted.
:: Bug fix - Use /OPTIONS instead of OPTIONS as a variable
:: name within the option parser so that it is unlikely to
:: collide with a user defined variable name.
:: 2016-01-14 v3.7: Reworked error handling a bit.
:: Bug fix - \xnn and \unnnn could fail in a regex search
:: if result was a meta-character and /X option was used.
:: 2015-07-15 v3.6: Added /?? option for paged help.
:: 2015-06-12 v3.5: Bug fix for $n or $nn in replace string when /T is
:: used without /J or /JMATCH or /L
:: 2015-01-22 v3.4: Bug fix - Use /TEST instead of TEST as a variable name
:: within the option parser so that it is unlikely to
:: collide with a user defined variable name.
:: 2014-12-24 v3.3: Bug fix for when /JMATCH is combined with /M or /S
:: 2014-12-09 v3.2: Bug fix for /T without /JMATCH - fixed dynamic repl func
:: Added GOTO at top for improved startup performance
:: 2014-11-25 v3.1: Added /JLIB option
:: Exception handler reports when regex is bad
:: Fix /X bug with extended ASCII
:: 2014-11-23 v3.0: Added /JBEGLN and /JENDLN options
:: Added skip, quit, and lpad() global variables/functions
:: Exception handler reports when error in user code
:: 2014-11-21 v2.2: Bug fix for /T with /L option.
:: 2014-11-20 v2.1: Bug fix for /T option when match is an empty string
:: 2014-11-17 v2.0: Added /T (translate) and /C (count input lines) options
:: 2014-11-14 v1.0: Initial release derived from REPL.BAT v6.2
::/
::============ Documentation ===========
::/INTRO
:::
:::JREPL Search Replace [/Option [Value]]...
:::JREPL /?[?][Topic|/Option|CHARSET/[Query]|HELP]
:::
::: Perform a global regular expression search and replace operation on
::: each line of ASCII input from stdin and prints the result to stdout.
:::
::: Each parameter may be optionally enclosed by double quotes. The double
::: quotes are not considered part of the argument. The quotes are required
::: if the parameter contains a batch token delimiter like space, tab, comma,
::: semicolon. The quotes should also be used if the argument contains a
::: batch special character like &, |, etc. so that the special character
::: does not need to be escaped with ^.
:::
::: Search - By default, this is a case sensitive JScript (ECMA) regular
::: expression expressed as a string.
:::
::: JScript regex syntax documentation is available at
::: https://msdn.microsoft.com/en-us/library/ae5bf541.aspx
:::
::: Replace - By default, this is the string to be used as a replacement for
::: each found search expression. Full support is provided for
::: substitution patterns available to the JScript replace method.
:::
::: For example, $& represents the portion of the source that matched
::: the entire search pattern, $1 represents the first captured
::: submatch, $2 the second captured submatch, etc. A $ literal
::: can be escaped as $$.
:::
::: An empty replacement string must be represented as "".
:::
::: Replace substitution pattern syntax is fully documented at
::: https://msdn.microsoft.com/en-US/library/efy6s3e6.aspx
:::
::: Binary input with NULL bytes requires either the /M option, or the file
::: must be read using ADO by appending the character set name to the file name.
::: For example, if your input is ASCII containing null bytes, then you must
::: use: /F "input.txt" /M or /F "input.txt|ascii".
:::
::: The meaning of extended ASCII byte codes >= 128 (0x80) is dependent on the
::: active code page. Extended ASCII within arguments and variables may require
::: the /XFILE option.
::/OPTIONS
:::
::: Options: Behavior may be altered by appending one or more options.
::: The option names are case insensitive, and may appear in any order
::: after the Replace argument.
:::
:: /A - write Altered lines only
:: /APP - Append results to the output file
:: /B - match Beginning of line
:: /C - Count number of source lines
:: /D - Delimiter for /N and /OFF
:: /E - match End of line
:: /EOL EndOfLineString - set the End Of Line terminator
:: /EXC BlockList - EXClude lines from selected blocks
:: /F InFile[|CharSet[|NB]]- read input from a File
:: /H - Highlight replaced or matched text
:: /HON HilightStart - defines the string to start highlighting
:: /HOFF HilightEnd - defines the string to stop highlighting
:: /HU - Underline replaced or matched text
:: /I - Ignore case
:: /INC BlockList - INClude lines from selected blocks
:: /J - JScript replace expressions
:: /JBEG InitCode - initialization JScript code
:: /JBEGLN NewLineCode - line initialization JScript code
:: /JEND FinalCode - finalization JScript code
:: /JENDLN EndLineCode - line finalization JScript code
:: /JLIB FileList - load file(s) of initialization code
:: /JMATCH - write matching JScript replacements only
:: /JMATCHQ - new Quick form of /JMATCH
:: /JQ - new Quick form of /J
:: /K Context or Pre:Post - search and Keep lines that match
:: /L - Literal search
:: /M - Multi-line mode
:: /MATCH - Search and print each match, one per line
:: /N MinWidth - prefix output with liNe numbers
:: /O OutFile[|CharSet[|NB]]- write Output to a file
:: /OFF MinWidth - add OFFsets to /K, /JMATCHQ, /MATCH output
:: /P Regex - only search/replace strings that match a Regex
:: /PFLAG Flags - set the /P regex Flags to "g", "gi", "" or "i"
:: /PREPL FilterReplCode - selectively Search/Replace captured /P groups
:: /R Context or Pre:Post - search and Reject lines that match
:: /RTN ReturnVar[:Line#] - Return result in a variable
:: /S VarName - Source is read from a variable
:: /T DelimChar or FILE - Translate multiple search/replace pairs
:: /TFLAG Flags - specify XRegExp flags for use with /T
:: /U - Unix line terminators (\n instead of \r\n)
:: /UTF - all input and output as UTF-16LE (BOM optional)
:: /V - use Variables for Search/Replace and code
:: /VT - enable Virtual Terminal ANSI escape sequences
:: /X - shorthand for combined /XFILE and /XSEQ
:: /XBYTES - force creation of new XBYTES.DAT
:: /XBYTESOFF - force /XSEQ \xnn to be treated as Windows-1252
:: /XFILE - preserve extended ASCII in args via temp files
:: /XREG FileList - adds XRegExp support to JREPL
:: /XSEQ - enable extended escape sequences
::/
::: /A - Only write altered lines. Unaltered lines are discarded.
::: If the /S option is used, then write the result only if
::: there was a change anywhere in the string. The /A option
::: is incompatible with the /M option unless the /S option
::: is also present.
:::
::: /APP - Modify /O behavior to Append results to the output file.
:::
::: /B - The Search must match the Beginning of a line.
::: Mostly used with literal searches.
:::
::: /C - Count the number of input lines and store the result in global
::: variable cnt. This value can be useful in JScript code associated
::: with any of the /Jxxx options.
:::
::: This option is implicitly enabled if /INC or /EXC contains a
::: negative value.
:::
::: This option is incompatible with the /M and /S options.
:::
::: If the input data is piped or redirected, then the data is first
::: written to a temporary file, so processing does not start until
::: the end-of-file is reached.
:::
::: /D Delimiter
:::
::: Specifies the Delimiter string to use after line numbers and/or
::: byte offsets that are output due to the /N or /OFF options.
::: The default value is a colon. The delimiter may be set to an
::: empty string by using /D "".
:::
::: /E - The Search must match the End of a line.
::: Mostly used with literal searches.
:::
::: /EOL EndOfLineString
:::
::: Write lines using EndOfLineString as the line terminator.
::: Standard JScript escape sequences may be used.
::: The default is "\r\n" (CarriageReturn LineFeed).
::: The value may be set to an empty string to eliminate linefeeds
::: from the output.
:::
::: /EOL has no effect if the /M option is used unless /MATCH,
::: /JMATCH, or /JMATCHQ is also used.
:::
::: Note that /EOL does not affect input.ReadLine or output.WriteLine
::: methods in user supplied JScript. ReadLine always accepts both
::: \r\n and \n as line terminators. And WriteLine always terminates
::: lines with \r\n.
:::
::: /EXC BlockList
:::
::: Exclude (do not search/replace) lines that appear within at least
::: one block within BlockList. A block may be a single line, or a
::: contiguous range of lines between a start and end line. The /EXC
::: option is incompatible with /M and /S.
:::
::: The syntax for specifying a BlockList is complex. Whitespace
::: should not appear anywhere except for possibly within a Regex or
::: String.
:::
::: BlockList = {Block}[,{Block}]...
::: {Block} = {SingleLine}|{LineRange}
::: {SingleLine} = {LineSpec}[{Offset}]
::: {LineRange} = {LineSpec}[{Offset}]:{EndLineSpec}[{Offset}]
::: {LineSpec} = [-]LineNumber|{Regex}[/]|{String}[/]
::: {EndLineSpec} = [-]LineNumber|+Number|{Regex}|{String}
::: {Regex} = /Regex/[i|b|e]...
::: {String} = 'String'[i|b|e]...
::: {Offset} = +Number|-Number
:::
::: A line may be identified by its LineNumber, or by a Regex that
::: matches the line, or a String literal that is found in the line.
::: Once identified, a line position may be adjusted forward or
::: backward via the optional Offset.
:::
::: A negative LineNumber is counted from the end of the file.
::: So 1 is the first line of input, and -1 is the last line. The /C
::: option is automatically activated if any block identifies a line
::: via a negative line number.
:::
::: A Block Regex uses mostly standard JScript syntax. Both a Regex
::: and a String may use any of the escape sequences defined by the
::: /XSEQ option, even if the /XSEQ option has not been set. Any /
::: literal within a Regex must be escaped as \/. Any ' literal
::: within a String must be escaped as ''.
:::
::: A Regex or String may be followed by any combination of the
::: following flags:
::: i - Ignore case
::: b - The search must match the beginning of a line
::: e - The search must match the end of a line
::: /EXC and /INC ignore the /I option.
:::
::: A line or range start that is identified by a Regex or String may
::: match multiple lines within the input. Only the first matching
::: line is used if the Regex or String is terminated by an extra /.
:::
::: A Line block or Range start that is identified by a Regex or
::: String cannot have a negative Offset.
:::
::: If the end of a Range is specified as + followed by a Number,
::: then the Number is treated as an offset from the start of the
::: Range (after any start Offset has been applied).
:::
::: If the end of a Range is specified as a Regex or String, then the
::: block end is the first line that matches after the beginning of
::: the block. The extra / cannot be used with an end of Range Regex
::: or String. The Offset must be greater than or equal to -1 if a
::: Regex or String is used. If the end of Range Regex or String is
::: not found, then the block continues to the end of file.
:::
::: Examples:
:::
::: /EXC "1:5,10,-5:-1"
::: Exclude the first 5, 10th, and last 5 lines.
:::
::: /EXC "/^:/"
::: Exclude all lines that begin with a colon
:::
::: /EXC "/^Begin$/+1:/^End$/-1"
::: Exclude all lines that are after a "Begin" line, and before
::: the next "End" line. Multiple blocks may be excluded.
:::
::: /EXC "/^DATA/i/:+10"
::: Exclude the first line that begins with DATA, ignoring case,
::: and exclude the next 10 lines as well.
:::
::: /EXC "'[START]':'[STOP]'"
::: Exclude lines beginning with a line that contains the literal
::: [START] and ending with the next line that contains [STOP].
:::
::: /EXC "'[START]'be:'[STOP]'be"
::: Exclude lines beginning with a [START] line (exact match)
::: and ending with the next [STOP] line (exact match).
:::
::: /F InFile[|CharSet[|NB]]
:::
::: Input is read from file InFile instead of stdin.
:::
::: If |CharSet (internet character set name) is appended to InFile,
::: then the file is opened via ADO using the specified CharSet value.
::: JREPL still recognizes both \n and \r\n as input line terminators
::: when using ADO. Both ADO and the CharSet must be available on the
::: local system. Appending |NB to the |CharSet normally has no impact.
::: The |NB No BOM flag is only useful when combined with /O -.
:::
::: Note: Input containing null bytes cannot be read unless ADO is
::: used, or else the /M option must be used.
:::
::: /H - Highlight all replaced or matched text in the output using the
::: strings defined by /HON and /HOFF.
:::
::: /HON and /HOFF default to ANSI escape sequences that swap the
::: foreground and background colors.
:::
::: /HU may be a better option if the current COLOR does not match
::: the console default.
:::
::: Native support for ANSI escape sequences requires Windows 10 or
::: higher. ANSI escape sequences only work on the Windows 10 console
::: if the "Use legacy console" option is off in console properties.
:::
::: In addition, one of the following must be used:
:::
::: - The /VT option can be used to enable the escape sequences for
::: a single JREPL run.
:::
::: or
:::
::: - The registry can have the following DWORD defined, which will
::: enable escape sequences for all console applications.
::: [HKEY_CURRENT_USER\Console]
::: "VirtualTerminalLevel"=dword:00000001
:::
::: /HON HighlightStart
:::
::: Defines the string to start highlighting text, normally an ANSI
::: escape sequence.
:::
::: Default is \x1B[7m - Swap foreground and background colors.
:::
::: /HOFF HighlightEnd
:::
::: Defines the string to end highlighting text, normally an ANSI
::: escape sequence.
:::
::: Default is \x1B[0m - Return to the console default format.
:::
::: /HU - Underline all replaced or matched text in the output using ANSI
::: escape sequences.
:::
::: This is the same as using /H with /HON=\x1B[4m and /HOFF=\x1B[24m
:::
::: /I - Ignore case when searching.
:::
::: /INC BlockList
:::
::: Only Include (search/replace) lines that appear within at least
::: one block within BlockList. A block may be a single line, or a
::: contiguous range of lines between a start and end line. The /INC
::: option is incompatible with /M and /S.
:::
::: A line within an /INC block is not included if it also appears
::: within an /EXC block.
:::
::: See the /EXC help for the syntax of a BlockList.
:::
::: Examples:
:::
::: /INC "1:5,10,-5:-1"
::: Include the first 5, 10th, and last 5 lines.
:::
::: /INC "/^:/"
::: Include all lines that begin with a colon
:::
::: /INC "/^Begin$/+1:/^End$/-1"
::: Include all lines that are after a "Begin" line, and before
::: the next "End" line. Multiple blocks may be included.
:::
::: /INC "/^DATA/i/:+10"
::: Include the first line that begins with DATA, ignoring case,
::: and include the next 10 lines as well.
:::
::: /INC "'[START]':'[STOP]'"
::: Include lines starting with a line that contains the literal
::: "[START]" and ending with the next line that contains "[STOP]".
:::
::: /INC "'[START]'be:'[STOP]'be"
::: Include lines beginning with a "[START]" line (exact match)
::: and ending with the next "[STOP]" line (exact match).
:::
::: /J - A deprecated form of /JQ that is slow because the JScript code
::: is executed via the eval() function each and every match. This
::: form does not use $txt - The replace value is taken as the value
::: of the last JScript expression within Replace.
:::
::: This option is only preserved so as not to break existing scripts.
:::
::: /JBEG InitCode
:::
::: JScript inititialization code to run prior to loading any input.
::: This is useful for initializing user defined variables for
::: accumulating information across matches. The default code is an
::: empty string.
:::
::: /JBEGLN NewLineCode
:::
::: JScript code to run at the beginning of each line, prior to
::: performing any search on the line. The line content may be
::: manipulated via the $txt variable. The default code is an empty
::: string. This option is incompatible with the /M and /S options.
:::
::: /JEND FinalCode
:::
::: JScript termination code to run when there is no more input to
::: read. This is useful for writing summarization results.
::: The default code is an empty string.
:::
::: /JENDLN EndLineCode
:::
::: JScript code to run at the end of each line, after all matches
::: on the line have been found, but before the result is printed.
::: The final result can be modified via the $txt variable. Setting
::: $txt to false discards the line without printing. The $txt value
::: is ignored if the /JMATCH option has been used. The default
::: code is an empty string. This option is incompatible with the
::: /M and /S options.
:::
::: /JLIB FileList
:::
::: Specifies one or more files that contain libraries of JScript
::: code to load before /JBEG is run. Multiple files are delimited
::: by forward slashes (/). Useful for declaring global variables
::: and functions in a way that is reusable.
:::
::: /JMATCH - A deprecated form of /JMATCHQ that is slow because the JScript
::: code is executed via the eval() function each and every match.
::: This form does not use $txt - The replace value is taken as the
::: value of the last JScript expression within Replace.
:::
::: This option is only preserved so as not to break existing scripts.
:::
::: /JMATCHQ - Write each Replace value on a new line, discarding all text
::: that does not match the Search. The Replace argument is one or
::: more JScript statements with access to the same $ variables
::: available to the /JQ option. The code must store the final replace
::: value in variable $txt. A $txt value of false indicates the match
::: is to be ignored.
:::
::: Note the trailing Q stands for Quick :-)
:::
::: /JQ - The Replace argument is one or more JScript statements that
::: define the replacement value, and possibly do more. The code
::: must store the final replace value in variable $txt.
:::
::: The following variables contain details about each match:
:::
::: $0 is the substring that matched the Search
::: $1 through $n are the captured submatch strings
::: $off is the offset where the match occurred
::: $src is the original source string
:::
::: Note the trailing Q stands for Quick :-)
:::
::: /K PreContext:PostContext[:FILE]
::: /K Context[:FILE]
::: /K COUNT[:FILE]
:::
::: Keep matches - Search and write out lines that contain at least
::: one match, without doing any replacement. The Replace argument is
::: still required, but is ignored.
:::
::: The integers PreContext and PostContext specify how many non-
::: matching lines to write before the match, and after the match,
::: respectively. If a single Context integer is given, then the same
::: number of non-matching lines are written before and after.
::: A Context of 0 writes only matching lines.
:::
::: If the value of Context is COUNT, then matching lines are counted
::: but not written, and the final match count is written at the end.
:::
::: If :FILE is appended to the context, then the Search parameter
::: specifies a file containing one or more search terms, one term
::: per line. A line matches if any of the search terms are found
::: witin the line. The file can be opened via ADO if |CharSet
::: (internet character set name) is appended to the file name.
::: Note: the /V option does not apply to Search if /K :FILE is used.
:::
::: See /OFF and /N for how to prefix each kept line with the match
::: byte offset and/or the line number.
:::
::: /K is incompatible with /A, /J, /JQ, /JMATCH, /JMATCHQ, /M,
::: /MATCH, /R, /S, and /T.
:::
::: /L - The Search is treated as a string literal instead of a
::: regular expression. Also, all $ found in the Replace string
::: are treated as $ literals.
:::
::: /M - Multi-line mode. The entire input is read and processed in one
::: pass instead of line by line, thus enabling search for \n. This
::: also enables preservation of the original line terminators.
::: The /M option is incompatible with the /A option unless the /S
::: option is also present.
:::
::: Note: The /M option is one method to read binary data with null
::: bytes. The other option is to use ADO to read the file.
::: See the /F option for more info.
:::
::: /MATCH - Search and write out each matching string on a new line,
::: discarding any non-matching text. The Replace argument is
::: usually ignored, but is still required. However, if the Replace
::: value is "COUNT", then the matches are counted but not written,
::: and the final match count is written at the end.
:::
::: See /OFF and /N for how to prefix each match with the byte offset
::: and/or the line number.
:::
::: /MATCH is incompatible with /A, /J, /JQ, /JMATCH, /JMATCHQ,
::: /K, /R and /T.
:::
::: /N MinWidth
:::
::: Precede each output line with the line number of the source line,
::: followed by a delimiter (colon by default). The default delimiter
::: can be overridden with the /D option.
:::
::: Line 1 is the first line of the source.
:::
::: The MinWidth value specifies the minimum number of digits to
::: display. The default value is 0, meaning do not display the
::: line number. A value of 1 displays the line numbers without any
::: zero padding.
:::
::: The /N option is ignored if the /M or /S option is used.
:::
::: /O OutFile[|CharSet[|NB]]
:::
::: Output is written to file OutFile instead of stdout. Any existing
::: OutFile is overwritten unless the /APP option is also used.
:::
::: If |CharSet (internet character set name) is appended to OutFile,
::: then the file is opened via ADO using the specified CharSet value.
::: The output line terminator still defaults to \r\n when using ADO,
::: and may be changed to \n with the \U option. Both ADO and the
::: CharSet must be available on the local system. Unicode files
::: written by ADO have a BOM by default. Appending |NB (or |anyvalue)
::: to the CharSet blocks the BOM from being written.
:::
::: If /F InFile is also used, then an OutFile value of "-" overwrites
::: the original InFile with the output. A value of "-" preserves the
::: original input character set (and also any |NB No BOM indicator).
::: A value of "-|" explicitly transforms the file into the machine
::: default character set. A "-|CharSet[|NB]" value explicitly
::: transforms the file into the specified character set. The output
::: is first written to a temporary file with the same path and name,
::: with .new appended. Upon completion, the temp file is moved to
::: replace the InFile.
:::
::: It is rarely useful, but /APP may be combined with /O -. But /APP
::: cannot be combined with /O "-|CharSet".
:::
::: /OFF MinWidth
:::
::: Ignored unless /JMATCHQ, /JMATCH, /MATCH, or /K is used.
::: Precede each line of output with the offset of the match within
::: the original source string, followed by a delimiter (colon by
::: default). The default delimiter can be overridden with the /D
::: option. The offset follows the line number if the /N option is
::: also used.
:::
::: Offset 0 is the first character of the source string. The source
::: string is normally the current line. But if the /M option is used
::: then the source string is the entire file.
:::
::: If used with /K, then the offset represents the first occurrence
::: of the search string within the line.
:::
::: The MinWidth value specifies the minimum number of digits to
::: display. The default value is 0, meaning do not display the
::: offset. A value of 1 displays the offsets without any zero
::: padding.
:::
::: /P FilterRegex
:::
::: Only Search/Replace strings that match the Pre-filter regular
::: expression FilterRegex. All escape sequences defined by /XSEQ are
::: available to FilterRegex, even if /XSEQ has not been set.
:::
::: FilterRegex is a global, case sensitive search by default.
::: The behavior may be changed via the /PFLAG option.
:::
::: By default, /P passes the entire matched filter string to the
::: main Search/Replace routine. If your FilterRegex includes captured
::: groups, then you can add the /PREPL option to selectively pass one
::: or more captured groups instead.
:::
::: The /P option ignores /I, but honors /M.
:::
::: The /P option may be combined with /INC and/or /EXC, in which case
::: /P is applied after lines have been included and/or excluded.
:::
::: Within the main Search argument, ^ matches the beginning of the
::: matched filter, and $ matches the end of the matched filter.
:::
::: Example - Substitute X for each character within curly braces,
::: including the braces.
:::
::: echo abc{xyz}def|jrepl . X /p "{.*?}"
:::
::: result:
:::
::: abcXXXXXdef
:::
::: See /PREPL for an example showing how to preserve the enclosing
::: braces.
:::
::: /PFLAG Flags
:::
::: Set the search flags to be used when defining the /P FilterRegex.
::: Possible values are:
::: "g" - global, case sensitive (default)
::: "gi" - global, ignore case
::: "" - first match only, case sensitive
::: "i" - first match only, ignore case
:::
::: If the search is not global, then the first match of each line
::: is used. If the /M option is used, then a non-global search uses
::: only the first match of the entire input.
:::
::: Note that the /P FilterRegex multiline mode is contolled by the
::: /M option. The "m" flag cannot be used with /PFLAG.
:::
::: /PREPL FilterReplaceCode
:::
::: Specify a JScript expression FilterReplaceCode that controls
::: what portion of the /P Pre-filter match is passed on to the main
::: Search/Replace routine, and what portion is preserved as-is.
:::
::: The expression is mostly standard JScript, and should evaluate to
::: a string value. $0 is the entire Pre-filter match, and $1 through
::: $N are the captured groups. The only non-standard syntax is the
::: use of curly braces to indicate what string expression gets passed
::: on to the main Search/Replace. Prior to executing the /P filter,
::: each brace expression within /PREPL is transformed as follows:
:::
::: {Expression} --> (Expression).replace(Search,Replace)
:::
::: Any JScript is allowed within /PREPL, except string literals
::: should not contain $, {, or }.
:::
::: Using /P without /PREPL is the same as using /P with /PREPL "{$0}"
:::
::: /PREPL cannot be used with /OFF.
:::
::: Note that neither /V nor /XFILE apply to /PREPL.
:::
::: Example - Substitute X for each character within curly braces,
::: excluding the braces.
:::
::: echo abc{xyz}def|jrepl . X /p "({)(.*?)(})" /prepl "$1+{$2}+$3"
:::
::: result:
:::
::: abc{XXX}def
:::
::: /R PreContext:PostContext[:FILE]
::: /R Context[:FILE]
::: /R COUNT[:FILE]
:::
::: Reject matches - Search and write out lines that do not contain
::: any matches, without doing any replacement. The Replace argument
::: is still required, but is ignored.
:::
::: The integers PreContext and PostContext specify how many matching
::: lines to write before the non-match, and after the non-match,
::: respectively. If a single Context integer is given, then the same
::: number of matching lines are written before and after.
::: A Context of 0 writes only non-matching lines.
:::
::: If the value of Context is COUNT, then rejected lines are counted
::: but not written, and the final reject count is written at the end.
:::
::: If :FILE is appended to the context, then the Search parameter
::: specifies a file containing one or more search terms, one term
::: per line. A line is rejected if any of the search terms are found
::: witin the line. The file can be opened via ADO if |CharSet
::: (internet character set name) is appended to the file name.
::: Note: the /V option does not apply to Search if /K :FILE is used.
:::
::: See /N for how to prefix each rejected line with the line number.
:::
::: /R is incomptaible with /A, /J, /JQ, /JMATCH, /JMATCHQ, /K, /M,
::: /MATCH, /S, and /T.
:::
::: /RTN ReturnVar[:[-]LineNumber]
:::
::: Write the result to variable ReturnVar.
:::
::: If the optional LineNumber is present, then only that specified
::: line within the result set is returned. A LineNumber of 1 is the
::: first line. A negative LineNumber is measured from the end of the
::: result set, so -1 is the last line. /RTN always breaks lines at
::: \r\n and \n - the /EOL value is ignored.
:::
::: All byte codes except NULL (0x00) are preserved, regardless
::: whether delayed expansion is enabled or not. An error is thrown
::: and no value stored if the result contains NULL.
:::
::: An error is thrown and no value stored if the value does not fit
::: within a variable. The maximum returned length varies depending
::: on the variable name and result content. The longest possible
::: returned length is 8179 bytes.
:::
::: The line terminator of the last match is suppressed if /MATCH,
::: /JMATCH, or /JMATCHQ is used. There is also no line terminator
::: if LineNumber is specified.
:::
::: /RTN uses a temporary output file to transfer the result to the
::: environment variable. By default the temporary file is written
::: as UTF-8. But the file is written using the CSCRIPT default code
::: page if the /XFILE option is used - the action may fail if the
::: result contains a character that cannot be mapped to the CSCRIPT
::: default code page.
:::
::: /S VarName
:::
::: The source is read from environment variable VarName instead
::: of from stdin. Without the /M option, ^ anchors the beginning
::: of the string, and $ the end of the string. With the /M option,
::: ^ anchors the beginning of a line, and $ the end of a line.
:::
::: The variable name must not begin with /.
:::
::: /T DelimiterChar
::: /T FILE
:::
::: The /T option is very similar to the Oracle Translate() function,
::: or the unix tr command, or the sed y command.
:::
::: The Search represents a set of search expressions, and Replace
::: is a like sized set of replacement expressions. Expressions are
::: delimited by DelimiterChar (a single character). If DelimiterChar
::: is an empty string, then each character is treated as its own
::: expression. The /L option is implicitly set if DelimiterChar is
::: empty. Normally escape sequences are interpreted after the search
::: and replace strings are split into expressions. But if the
::: DelimiterChar is empty and /XSEQ is used, then escape sequences
::: are interpreted prior to the split at every character.
:::
::: An alternate syntax is to specify the word FILE instead of a
::: DelimiterChar, in which case the Search and Replace parameters
::: specify files that contain the search and replace expressions,
::: one expression per line. Each file can be opened via ADO if
::: |CharSet (internet character set name) is appended to the file
::: name. Note that the /V option does not apply to Search and Replace
::: if /T FILE is used.
:::
::: Each substring from the input that matches a particular search
::: expression is translated into the corresponding replacement
::: expression.
:::
::: The search expressions may be regular expressions, possibly with
::: captured groups. Note that each expression is itself converted into
::: a captured group behind the scene, and the operation is performed
::: as a single search/replace upon execution. So backreferences within
::: each regex, and $n references within each replacement expression,
::: must be adjusted accordingly. The total number of expressions plus
::: captured groups must not exceed 99.
:::
::: If an expression must include a delimiter, then an escape
::: sequence must be used (not an issue if the FILE syntax is used).
:::
::: Search expressions are tested from left to right. The left most
::: matching expression takes precedence when there are multiple
::: matching expressions.
:::
::: Examples using /T:
:::
::: ROT13 - Simple character substitution is achieved by setting the
::: /T delimiter to an empty string. The search and replace strings
::: must have identical length. The use of line continuation aligns
::: the replace string directly below the search string, thus making
::: it very easy to see exactly how each character will be translated.
::: The "a" in the search string will be replaced by the "n" in the
::: replace string. And you can see the symmetry in that the "n" will
::: be replaced by "a".
:::
::: echo The quick brown fox jumps over a lazy dog | jrepl^
::: "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"^
::: "nopqrstuvwxyzabcdefghijklmNOPQRSTUVWXYZABCDEFGHIJKLM"^
::: /t ""
:::
::: -- OUTPUT --
:::
::: Gur dhvpx oebja sbk whzcf bire n ynml qbt
:::
::: Simple string substitution - The /T option specifies that string
::: expressions are delimited by a space. The /L option prevents "."
::: from being interpreted as a regex wildcard character.
:::
::: echo The blackbird flew through the blue sky. | jrepl^
::: "black blue sky ." "blue black night !" /l /t " "
:::
::: -- OUTPUT--
:::
::: The bluebird flew through the black night!
:::
::: Simple string substitution using FILE - This is the same as the
::: prior example, except now the Search and Replace strings are in
::: the following files:
:::
::: find.txt repl.txt
::: -------- --------
::: black blue
::: blue black
::: sky night
::: . !
:::
::: The following command yields the same output as before:
:::
::: echo The blackbird flew through the blue sky. | jrepl^
::: find.txt repl.txt /l /t file
:::
::: Pig Latin - This example shows how /T can be used with regular
::: expressions, and it demonstrates how the numbering of captured
::: groups must be adjusted. The /T delimiter is set to a space.
:::
::: The first regex is captured as $1, and it matches words that begin
::: with a consonant. The first captured group ($2) contains the initial
::: sequence of consonants, and the second captured group ($3) contains
::: the balance of the word. The corresponding replacement string moves
::: $2 after $3, with a "-" in between, and appends "ay".
:::
::: The second regex matches any word, and it is captured as $4 because
::: the prior regex ended with group $3. Because the first regex matched
::: all words that begin with consonants, the only thing the second
::: regex can match is a word that begins with a vowel. The replacement
::: string simply adds "-yay" to the end of $4. Note that $& could have
::: been used instead of $4, and it would yield the same result.
:::
::: echo Can you speak Pig Latin? | jrepl^
::: "\b((?:qu(?=[aeiou])|[bcdfghj-np-twxz])+)([a-z']+)\b \b[a-z']+\b"^
::: "$3-$2ay $4-yay" /t " " /i
:::
::: -- OUTPUT --
:::
::: an-Cay you-yay eak-spay ig-Pay atin-Lay?
:::
::: Pig-Latin with proper capitalization - This is simply an extension
::: of the prior example. The /JBEG option defines a fixCaps() function
::: that checks if the translated word is all lower case, except for one
::: capital letter after the "-". If so, then the initial letter is
::: capitalized, and the remainder is converted to lower caae. The /JQ
::: option treats the replacement strings as JScript expressions. The
::: first replacement expression uses fixCaps() to properly restore case.
:::
::: echo Can you speak Pig Latin? | jrepl^
::: "\b((?:qu(?=[aeiou])|[bcdfghj-np-twxz])+)([a-z']+)\b \b[a-z']+\b"^
::: "$txt=fixCaps($3+'-'+$2+'ay') $txt=$4+'-yay'"^
::: /t " " /i /j /jbeg ^"^
::: function fixCaps(str){^
::: return str.search(/[a-z']+-[A-Z][a-z]*$/)==0 ?^
::: str.substr(0,1).toUpperCase()+str.substr(1).toLowerCase() : str^
::: }^"
:::
::: -- OUTPUT --
:::
::: An-cay you-yay eak-spay Ig-pay Atin-lay?
:::
::: /TFLAG Flags
:::
::: Used to specify XRegExp non-standard mode flags for use with /T.
::: /TFLAG is ignored unless both /T and /XREG are used.
:::
::: /U - Write lines using a Unix line terminator \n instead of Windows
::: terminator of \r\n. This is the same as using /EOL "\n".
::: See /EOL help for more info.
:::
::: /UTF - All input and output encodings are Unicode UTF-16 Little
::: Endian (UTF-16LE). This includes stdin and stdout. The only
::: exceptions are /JLIB and /XREG files, which are still read
::: as ASCII.
:::
::: The \xFF\xFE BOM is optional for input.
:::
::: Output files will automatically have the \xFF\xFE BOM inserted.
::: But stdout will not have the BOM.
:::
::: Regular expression support of Unicode can be improved by using
::: the /XREG option.
:::
::: Variables are never written to temporary files (/XFILE is ignored)
::: if /UTF is used.
:::
::: Unfortunately, /UTF is incompatible with /RTN.
:::
::: /V - Search, Replace, /INC BlockList, /EXC BlockList, /P FilterRegex,
::: /JBEG InitCode, /JBEGLN NewLineCode, /JEND FinalCode, and
::: /JENDLN EndLineCode all represent the names of environment
::: variables that contain the respective values. An undefined
::: variable is treated as an empty string.
:::
::: Variable names beginning with / are reserved for option storage
::: and other internal uses. So user defined variables used with /V
::: must not have a name that begins with /.
:::
::: /VT - Enables Virtual Terminal processing of ANSI escape sequences for
::: the current JREPL.bat process within the Windows 10 console. This
::: option is not needed if the registry has HKEY_CURRENT_USER\Console
::: "VirtualTerminalLevel" DWORD set to 1 in the registry.
:::
::: /X - Shorthand for combined /XFILE and /XSEQ.
:::
::: /XBYTES - Force creation of a new XBYTES.DAT file for use by the /XSEQ
::: option when decoding \xnn sequences.
:::
::: /XBYTESOFF - Force JREPL to use pre v7.4 behavior where /XSEQ \xnn is
::: always interpreted as Windows-1252.
:::
::: /XFILE - Preserves extended ASCII characters that may appear within