Computer Hope
Microsoft => Microsoft DOS => Topic started 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
)
-
use findstr . type findstr /? to find out more
-
But how do incorporate the findstr command into the 'for' loop?
-
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.
-
This might work for you:
@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.
-
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
@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:\>
-
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:\>
-
...
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
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
findstr [blah options] "text_to_find" >>newfile
-
ghost,
You are right the for loop is not needed:
@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.
-
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.