reading files and substrings

May 6, 2010 at 12:30:16
Specs: dos
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.

See More: reading files and substrings

Report •

May 6, 2010 at 13:14:54
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

Report •

May 9, 2010 at 05:42:58
Suggested reading:
findstr /?

Report •

May 9, 2010 at 13:25:08
here's using findstr:
@echo off>result
for /f "tokens=2* delims=:" %%a in ('findstr /r "^SHP.*12345" *.txt') do >> result echo %%a

Report •

Related Solutions

May 9, 2010 at 15:09:15
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 ">".

Report •

Ask Question