Extracting certain lines from text files

December 8, 2010 at 08:24:10
Specs: Windows Vista
I have several text files to read though. Each has a lot of lines, but I only need to pull lines starting with "Rule Title" out and save them all to 1 file if possible. Example:

file1.txt

this is file1 inside
Rule Title : I want to save this
this is junk

file2.txt

this is file2 inside
Rule Title : I want to save this
this is junk

Normally to do this I use dos commands and something like "type file1.txt | find "Rule Title" > newfile1.txt

I'm trying to get a way to input multiple text files without typing each name in, searching for lines starting with that particular string ("Rule Title:") and then saving them all to 1 file.

thanks


See More: Extracting certain lines from text files

Report •


#1
December 8, 2010 at 10:31:08
@echo off > newfile & setLocal enableDELAYedeXpansion

for /f "tokens=* delims= " %%a in ('dir/b *.txt') do (
findstr /b "Rule Title" < %%a >> newfile
)


=====================================
Life is too important to be taken seriously.

M2


Report •

#2
December 8, 2010 at 10:40:15
if all the files are in same directory and you want to search all text files then multiple ways.

1) Just modify your command a little bit ...
type *.txt | find "Rule Title" >> newfile1.txt

2)Use for loop for all the files in that folder..

for /f %a in ('dir /b *.txt') do type "%a" | find "Rule Title" >> newfile2.txt

3) Use for loop for a list of files, suppose list.txt contain the list of files you wanna search, it could be the file name or full path including quotes.

for /f %a in (list.txt) do type "%a" | find "Rule Title" >> newfile2.txt

* If you use above commands in batch file then use %% instead of single %.

Subhash Chandra.


Report •

#3
December 8, 2010 at 10:41:34
Sorry M2,
I didn't seen your reply before i hit Submit :(

Subhash Chandra.


Report •

Related Solutions

#4
December 8, 2010 at 11:16:20
Wow you guys are really fast and I really appreciate your help. How can I also add the name of the file before each file is processed so that the final result will be:

file1.txt

Rule Title: yada yada

file2.txt

Rule Title: yada yada

file3.txt

Rule Title: yada yada

thanks!

JD


Report •

#5
December 8, 2010 at 11:21:26
@echo off > newfile & setLocal enableDELAYedeXpansion

for /f "tokens=* delims= " %%a in ('dir/b *.txt') do (
>> newfile echo.%%a
findstr /b "Rule Title" < %%a >> newfile
)


=====================================
Life is too important to be taken seriously.

M2


Report •

#6
December 8, 2010 at 12:54:18
This is actually showing any line that starts with "Rule" and not just "Rule Title:"

This site is really nice. I was trying to do this with a nested for loop. One going through each file name and the other going through each file itself. That's what I would have done in java probably :)

JD


Report •

#7
December 8, 2010 at 14:10:17
oops... You're right. I never use findstr. I use a ported grep.

=========================================

@echo off > newfile & setLocal enableDELAYedeXpansion

for /f "tokens=* delims= " %%a in ('dir/b *.txt') do (
>> newfile echo.%%a
findstr /b /c:"Rule Title" < %%a >> newfile
)


=====================================
Life is too important to be taken seriously.

M2


Report •

#8
December 8, 2010 at 14:49:58
using loops:

==============================================
@echo off > newfile & setLocal enableDELAYedeXpansion

for /f "tokens=* delims= " %%a in ('dir/b *.txt') do (
>> newfile echo.%%a
for /f "tokens=* delims= " %%i in (%%a) do (
set S=%%i
set R=!S:~0,10!
if "!R!" equ "Rule Title" >> newfile echo.!S!
)
)


=====================================
Life is too important to be taken seriously.

M2


Report •

#9
December 8, 2010 at 14:49:59
using loops:

==============================================
@echo off > newfile & setLocal enableDELAYedeXpansion

for /f "tokens=* delims= " %%a in ('dir/b *.txt') do (
>> newfile echo.%%a
for /f "tokens=* delims= " %%i in (%%a) do (
set S=%%i
set R=!S:~0,10!
if "!R!" equ "Rule Title" >> newfile echo.!S!
)
)


=====================================
Life is too important to be taken seriously.

M2


Report •


Ask Question