I need a simple throw away dos script that will read all the .txt files in a dir and output lines from each if they contain certain text. For example each file contains multiple lines that start with SHP, DTL, DTP, etc and if they start with SHP and contain string "12345", I want to output them to another file. So I will end up with one file containing all the appropriate strings from the .txt files. I've gotten it to read thru all the files in a dir, and to echo the complete lines in a specific file, but I am now stuck.
I have echo %%a:0,3% >> outfile and it's doing something weird.
If you've got any sample that does something like this, I can modify it to work. Thanks.
Here's what I have so far and it puts out every record in every file with the file name at the start. I just need the string manipulation to pull out parts of the %%b so that I can ouput selected records. @echo off
for %%a in (*.txt) do (
for /f "tokens=1 delims=" %%b in (%%a) do (
echo %%a " " %%b >> outputfile
)
)
Suggested reading: findstr /?
here's using findstr:
@echo off>result
for /f "tokens=2* delims=:" %%a in ('findstr /r "^SHP.*12345" *.txt') do >> result echo %%a
Get a copy of the "grep" utility from a Free Software site. Your sample search would be grep "^SHP.*12345" *.txt
The search pattern ("regular expression") says to match "SHP" at the beginning of a line, discard 0 or more characters, and match "12345" later on.
If you want to save the results of the search, you just redirect it into a file with ">".
Yes (14) | ![]() | |
No (14) | ![]() | |
I don't know (15) | ![]() |