I have to filter a text file filter.tmp containing two types of lines, this shows the difference:
findstr /r "^[0-9][0-9]*.*$" filter.tmp > filter-numbers.tmp
findstr /r "^[^0-9][^0-9]*.*$" filter.tmp > filter-text.tmp
What I need to do is to append lines containing text together like this and if line does contain number just put it to output file:
IF "current line" contains text THEN
previous line = concatenate "previous line" + "/" + "current line"
ELSE
echo "previous line" >> filter.out
echo "current line" >> filter.out
filter.tmp contains something like:
Hello
World
Foo
Bar
45: this is some line
Trouble
with code
66: another line
filter.out should look like:
Hello/World/Foo/Bar
45: this is some line
Trouble/with code
66: another line
I realize, this is very simple, but I just can not get it working. As I am thinking about it, it would be much easier to use C++….
This is a quite verbatim translation of your pseudocode and your regexes, based on the assumption that »contains numbers« really means »starts with two digits« (which is what your regexes show):
This uses several things. First of all, we need delayed expansion which enables us to manipulate environment variables within the loop. Then we iterate over the lines in the file with
for /f. Note that this will skip empty lines in the file, but you cannot avoid that. Inside thefor /floop the variableLineholds the current line andPrevthe previous one (if there has been a previous one). I swapped thethenandelsebranches of the condition since numbers at the start of the line are easier to check for than non-numbers.With the
echoyou’ll notice that I moved the redirection to the start of the line; this is to prevent trailing numbers inPrevorLinefrom having an effect on the redirection (and also to avoid trailing spaces).If you’re not adverse to PowerShell, you can use the following: