The first code only works if the number of pipes is constant and the number of dashes is constant.
The second code still requires the number of pipes to be constant, but the number of dashes can vary.
A variable like %%G within a batch file is a FOR variable. If used on the command line, then it would be %G instead.
FOR loops can be very confusing for beginners because slight changes in syntax can result in totally different function. Getting the exact behavior you want is often difficult and/or tedious even for experienced users.
A simple FOR iterates strings and or files, depending on whether there are wildcards within the IN() clause.
FOR /F parses one or more tokens from each line that it iterates, with a different %%_ variable for each token. The lines that are iterated can either be the content of a file, the output of a command, or a string literal, depending on the syntax used within the IN() clause.
You should carefully read the help text that you get when you execute FOR /? from the command line and try some experiments on your own.
Both of Squashman's codes could be combined with your FINDSTR and eliminate your intermediate file if you change the outer FOR /F loop as follows:
Code: Select all
(FOR /F "TOKENS=1,5,6 DELIMS=|" %%G IN ('findstr "yourSearchHere" input.log') DO (
However, if the output of FINDSTR is huge, then it is much faster to preserve the two steps and use the temporary intermediate file.
Aacini's FINDREPL is a sophisticated utility. I'm sure it could easily meet your requirements, but I haven't taken the time to learn how to use FINDREPL.
My JREPL.BAT utility
serves a similar purpose, but with totally different syntax.
JREPL can solve your problem with a single, fairly short one liner:
Code: Select all
call jrepl "\|.*(\|.*?\|) -.*-" "$2 -" /t " " /inc "/XXX/" /a /f original.log /o output.txt
The first argument represents a pair of regular expression search terms, delimited by a space. The second argument specifies the corresponding replacement terms, again delimited by a space. The /T " " option is what dictates that the arguments are space delimited lists of find/replace terms.
The capture group numbering is a bit tricky because each search term is converted into its own captured group behind the scenes. The explicit captured group in the first term is actually group 2 because the first term is group 1. The second term becomes group 3.
The /INC "/XXX/" option specifies which lines should be included in the FIND/REPLACE operation. The XXX represents a regular expression that you would have to supply to take the place of the FINDSTR that you are currently using.
The /A option causes only lines that have been altered to be output, thus preventing the lines that don't match the /INC regular expression to be excluded from the output.
The /F original.log option specifies your input, and /O output.txt specifies your output.
If you want to continue using FINDSTR to filter the lines, then you only need the following
Code: Select all
call jrepl "\|.*(\|.*?\|) -.*-" "$2 -" /t " " /f input.txt /o output.txt
If you have additional questions regarding Squashman's code, or my JREPL solution, then I suggest you start a new topic. I don't want to hijack Aacini's FINDREPL topic anymore than I already have.