Computer Hope

Microsoft => Microsoft DOS => Topic started by: gkata on June 11, 2009, 06:35:55 AM

Title: dos for command
Post by: gkata on June 11, 2009, 06:35:55 AM
I have a file log file for which I need to read each line.  If a line in the log file starts with the word 'Record' or 'ORA' I want to capture the complete line and write it to a new file.

I have this, but it doesn't work correctly:

for /f %%a in (main.log) do (
echo %%a >> newfile.txt
)
Title: Re: dos for command
Post by: gh0std0g74 on June 11, 2009, 08:41:58 AM
use findstr . type findstr /? to find out more
Title: Re: dos for command
Post by: gkata on June 11, 2009, 08:49:43 AM
But how do incorporate the findstr command into the 'for' loop?
Title: Re: dos for command
Post by: gh0std0g74 on June 11, 2009, 09:44:25 AM
sorry dude, i don't do batch.
you can search for similar solutions in the forum (there are plenty) , or you can wait for the batch enthusiast to give you a hand.

Title: Re: dos for command
Post by: Woodman on June 12, 2009, 05:34:28 AM
This might work for you:

Code: [Select]
@echo off > newfile.txt
cls
setlocal enabledelayedexpansion

for /f "delims=*" %%A in (main.log) do (
    set line=%%A & call :findit
)
exit /b

:findit
for /f %%B in ("!line!") do (
    if %%B equ Record echo !line!>>newfile.txt
    if %%B equ ORA echo !line!>>newfile.txt
)

Good luck.

Title: Re: dos for command
Post by: billrich on June 12, 2009, 06:42:34 AM
gkata,

( The following code works. I have tested and retested the code. )

C:\>type mainlog.txt
Record  bbbbbbbbbbbbbbbbbb
ORA  xxxxxxxxxxxxxxxxxxxxxxxx
Now is the time to find

C:\>type orastr.bat
Code: [Select]
@echo off

setlocal enabledelayedexpansion
echo Hello > newfile.txt

for /f "delims=*" %%A in (mainlog.txt) do (
echo %%A  |  findstr  "ORA Record" >> newfile.txt

)
type newfile.txt

Output:

C:\>type newfile.txt
Hello
Record  bbbbbbbbbbbbbbbbbb
ORA  xxxxxxxxxxxxxxxxxxxxxxxx

C:\>
Title: Re: dos for command
Post by: billrich on June 12, 2009, 06:51:29 AM
gkata,


C:\>findstr /?
Searches for strings in files.
.
.
.

 
 For example : 'FINDSTR "hello there" x.txt' searches for "hello" or
"there" in file x.txt.
.
.
.

C:\>
Title: Re: dos for command
Post by: gh0std0g74 on June 12, 2009, 06:53:31 AM
...
for /f "delims=*" %%A in (mainlog.txt) do (
echo %%A  |  findstr  "ORA Record" >> newfile.txt
....
the above method calls findstr for each line iteration. If there are 10000 lines, then findstr will be called 10000  times, which is inefficient.
better to use it the other way round , ie
Code: [Select]
for .....  ( findstr "ORA Record" mainlog.txt ) do blah blah
if there are further processing on each line found, or simply pipe to newfile if what OP wants is only to find ORA records
Code: [Select]
findstr [blah options] "text_to_find" >>newfile
Title: Re: dos for command
Post by: billrich on June 12, 2009, 07:42:31 AM
ghost,

You are right the for loop is  not needed:

Code: [Select]
@echo off
echo. > newnewfile.txt
findstr  "ORA Record"  mainlog.txt  >>  newnewfile.txt

type  newnewfile.txt

Output:


C:\>type newnewfile.txt

Record  bbbbbbbbbbbbbbbbbb
ORA  xxxxxxxxxxxxxxxxxxxxxxxx

C:\>

p.s. Works just like the Unix grep command.

Title: Re: dos for command
Post by: gh0std0g74 on June 12, 2009, 10:11:03 AM
p.s. Works just like the Unix grep command.
and also awk/sed. these tools iterate files and find patterns so they are similar. however, awk goes a bit more in that it is a programming language by itself. therefore, one does not need to learn grep/sed and use only awk to do the job( of many , eg cut, wc). this will reduce your learning curve a lot.