Batch to find a string of number&letter combinatio

2019-07-19 08:26发布

问题:

I have a question regarding batch file. I need a batch file which will find a string of number&letter combination in only first 10 letters per row (in a multiple .txt files) and print out the whole row into an output .txt file. At the moment i have been using this:

@echo off  
findstr "A1234 B5678" "*.txt"  
for /f "delims=" %%A in ('findstr "A1234 B5678" "*.txt"') do (echo %%A >> OUTPUTFILE.txt) 

So what I am looking for in above is a combination of letters A1234 and B5678 (just an example) in all txt files in directory and printing them out into outputfile.txt. It works out just fine but i need to improve this a little. I only want batch to find this combination (A1234 and B5678) in only first 10 letters per row and print the whole row into an output txt file. For Example we would have a multiple txt files which would have in themself lines like these:

00 A1234 QWERTZ  
AA B5678 ASDFGH

Batch would check all the files for string combination A1234 and B5678 and would print out the whole row which had that combination in them.
I hope you guys will understand me, because my english is not the best. Also I am not very much experienced in cmd so if possible, please make it as easy(understandable) as possible :).
Thanks alot!

回答1:

There is another way to do it that is a bit more complicated, but it does not rely on extra code. It slices all txt files in the current directory into lines. The first 10 characters of these lines are feed into findstr. If findstr succeeds it echos the line to the console.

Use the following batch file:

@echo off
setlocal
for /f "delims=" %%I in ('dir/B *.txt') do (
    for /f "delims=" %%J in (%%I) do (
        set "_=%%J"
        call echo %%_:~0,10%%|findstr "PATTERN1 PATTERN2"&&call echo %%_%%
    )
)
endlocal
goto:eof

For the cracks: I know there is ENABLEDELAYEDEXPANSION. So the call style could be avoided by using

setlocal ENABLEDELAYEDEXPANSION

and

        echo !_:~0,10!|findstr "PATERN1 PATERN2"&&call echo !_!

But I deem this as bad style, because this tends to cause problems, especially if anything you want to echo includes '!' So I always use the call and double %% style in my examples, except the DELAYEDEXPANSION cannot be avoided.



回答2:

If you are dead set on using cmd, you can use findstr with the regex flag as follows:

C:\>type test.txt
00 A1234 QWERTZ
AA B5678 ASDFGH
BB A1234 B5678
00 A1234 QWERTZ
AA B5678 ASDFGH
BB A1234 B5678
00 A1234 QWERTZ
AA B5678 ASDFGH
BB A1234 B5678
00 A1234 QWERTZ
AA B5678 ASDFGH
BB A1234 B5678
00 A1234 QWERTZ
AA B5678 ASDFGH
BB A1234 B5678
00 A1234 QWERTZ
AA B5678 ASDFGH
BB A1234 B5678
00 A1234 QWERTZ
AA B5678 ASDFGH
BB A1234 B5678

C:\>findstr /R "^*[A][0-9]* [B][0-9]*$" test.txt
BB A1234 B5678
BB A1234 B5678
BB A1234 B5678
BB A1234 B5678
BB A1234 B5678
BB A1234 B5678
BB A1234 B5678

-- ab1



回答3:

If I got you right, you want to search in the first 10 characters for multiple specific alphanumeric combinations. I think you can do this with a regular expression, but unfortunately findstr cannot handle alternative patterns.

findstr /r "(pattern1|pattern2)"

does not work. So you have to branch out to grep for windows which can be found here The syntax for n patterns is:

grep -h -E -e "^.. ({PATTERN 1}|{PATTERN 2}|...|{PATTERN 2})" *.txt

So for your example you can use:

grep -h -E -e "^.. (A1234|B5678)" *.txt

search in every *.txt for any line that begins with 2 random letters followed by a space followed by A1234 or B5678 and prints out this line. The -h in the beginning supresses the output of the filename of the line.



回答4:

This uses a helper batch file called findrepl.bat which is similar to grep but runs as a plain batch file - it can be found here - http://www.dostips.com/forum/viewtopic.php?f=3&t=4697

findrepl.bat uses jscript in the Windows Scripting Host and it will work unless your IT admin have disabled WSH.

@echo off 
for %%a in (*.txt) do (
echo ============="%%a"================>>search
type "%%a"|findrepl "^.. (A1234|B5678)">>search 
)
ren search search.txt