热门排行
简介
JREPL.BAT - regex text processor ,可以在dos命令下通过正则表达式匹配txt中的内容,类似linux的命令行工具了,喜欢windows界面的朋友可以查看脚本之家的在线测试工具支持高亮。
Here is the current version of JREPL.BAT. Read the subsequent posts for examples of usage and to follow the development history.
JREPL.BAT is a powerful, general purpose, command line, regular expression text processor for ASCII data. It is a hybrid JScript/batch script that should run on any Windows machine from XP onward.
Here is a trivial example that will substitute "blue" for every occurrence of the word "red" within a file. Changes are made directly to the file:
jrepl "\bred\b" "blue" /f test.txt /o -
If the command is used in a batch script, then CALL must be used so that the calling script can continue after JREPL finishes.
Full documentation can be accessed by using the following:
jrepl /?
JREPL.BAT is a direct descendent from REPL.BAT. A new name was needed because the calling syntax is not backward compatible.
JREPL.BAT uses the same options as REPL.BAT, except instead of concatenating all the options into one string, each option must be listed separately with a slash prefix.
Besides having different calling syntax, JREPL version 1.0 offers the following enhancements over REPL.BAT version 6:
The input file may be specified using /F "file". No need to pipe or redirect the input file.
The output may be sent to a file using /O "file". No need to redirect the output.
/O "-" will replace the original file with the result. The output is first written to a temporary file, and then it is MOVEd to replace the original.
/JMATCH discards all non-matching text, and each match's replacement value is written on a new line. The replacement value is expressed as JScript code.
/JBEG "code" and /JEND "code" run initialization and termination JSCRIPT code supplied by the user. User defined global variables can be declared and initialized within the /JBEG code. Then the /J or /JMATCH Replacement code can update the variables upon each match. Finally, the /JEND code can write summarized information upon completion.
/N "width" prefixes each line of output with the corresponding line number from the input. The line numbers may be zero padded to a minimum width.
/OFF "width" prefixes each line of /JMATCH output with the offset within the source line where the match occurred. The offsets may be zero padded to a minimum width.
Expanded the number of predefined global variables/methods/objects that may be used by user supplied JScript code.
All global variables/methods/objects used by the JREPL script itself are hidden behind a single opaque global object named "_g". User supplied code can create identifiers without fear of corrupting the JREPL script as long as the _g object is avoided.
Please let me know if you find any bugs - I've done rudimentary testing, but I do not yet have a regression test plan, and the code has grown substantially since the original version of REPL.BAT. I may have unknowingly broken code that worked previously.
Full documentation is available from the command line via JREPL /?, or JREPL /?? for paged help.
The entire documentation is also listed at the top of the code, after the history.
Unfortunately the code has gotten too big to post on this site as view-able text. More than half the code is documentation :!:
Index of releases
v6.6 New /RTN option to store result in a variable. Improved support for extended ASCII. Fixed documentation error introduced by version 6.0
v6.4 Improved peformance by dynamically building optimized main loop based on chosen options
v6.2 Added /K /R /MATCH /P /PFLAG /JQ /JQMATCH. Improved /INC /EXC. Improved Performance.
v5.2 Added /T FILE option
v5.0 Added /U option
v4.5 Added /D option
v4.4 Bug fix for /C when last line missing \n
v4.3 Added rpad() and improved lpad()
v4.2 Improved /?Options help
v4.1 Added help for single option/topic as well as /T examples
v4.0 Added /INC and /EXC
v3.8 Bug fix - hide some additional internal variables
v3.7 Bug fix for \xnn and \unnnn in regex with /X
v3.6 Added /?? paged help option
v3.5 /T bug fix for $n and $nn when not /J or /JMATCH
v3.4 "Hide" internal /TEST variable (instead of TEST)
v3.3 Added /JLIB plus some bug fixes
v3.0 Added /JBEGLN and /JENDLN
v2.2 Added /T option
v1.0 Initial release
Dave Benham