MS-DOS: search & extract data from a file

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
BatchIt
Posts: 6
Joined: 03 Oct 2007 15:04

MS-DOS: search & extract data from a file

#1 Post by BatchIt » 03 Oct 2007 15:08

Hello,

I have a file that I want to search and extract portions of the data and send to new files, one for each portion.

A section of the original file I want to search and extract data from is below.

So how do I effectively use findstr to accomplish this? How would I search for and extract data from 8/21/07.... to the end of that section: 'High Temp: 93'?

Can someone help with this?

-BatchIt

------------
8/20/07 DAY: 1 logging information

range: 12 - 15

Temperature: 89
Temperature: 87
Temperature: 91
Temperature: 88
range: 12 - 15

Low Temp: 55
Low Temp: 51
Low Temp: 48
Low Temp: 59
range: 12 - 15

High Temp: 94
High Temp: 92
High Temp: 96
High Temp: 97

8/21/07 DAY: 2 logging information

range: 12 - 15

Temperature: 79
Temperature: 81
Temperature: 89
Temperature: 90
range: 12 - 15

Low Temp: 58
Low Temp: 48
Low Temp: 50
Low Temp: 57
range: 12 - 15

High Temp: 94
High Temp: 99
High Temp: 94
High Temp: 93
------------

jeb
Expert
Posts: 1055
Joined: 30 Aug 2007 08:05
Location: Germany, Bochum

#2 Post by jeb » 05 Oct 2007 14:09

Hi,

try this.

It parse the file, and per day it creates a new file with filename like
8-20-07.part

@echo off
SETLOCAL ENABLEEXTENSIONS
SETLOCAL ENABLEDELAYEDEXPANSION
set filename=nothing

for /f "tokens=*" %%a IN (%~1) DO (
call :parseDay "%%a" filename

echo %%a >> !filename!
)

goto :eof


::::::::::::::::::::::
:parseDay
for /f "tokens=1,2" %%a in (%1) do (
if "%%b"=="DAY:" (
set filename=%%a.day
set filename=!filename:/=-!
)
)
goto :eof


hope it helps

jeb

BatchIt
Posts: 6
Joined: 03 Oct 2007 15:04

#3 Post by BatchIt » 08 Oct 2007 11:41

Thx Jeb,

I am trying to understand this part of the script:
set filename=!filename:/=-!

What do the following do?
!
:
/
=-
Last edited by BatchIt on 08 Oct 2007 13:24, edited 1 time in total.

BatchIt
Posts: 6
Joined: 03 Oct 2007 15:04

#4 Post by BatchIt » 08 Oct 2007 13:23

Hey Jeb,

I had a chance to run the batch and review the output files. Its a gives me a good jump start. If I wanted the batch to do something a little different such as:
search and create these subfiles:

subfile 1:
8/20/07 DAY: 1 logging information

range: 12 - 15

Temperature: 89
Temperature: 87
Temperature: 91
Temperature: 88

subfile 2:
8/20/07 DAY: 1 logging information

range: 12 - 15

Low Temp: 55
Low Temp: 51
Low Temp: 48
Low Temp: 59

subfile 3:
8/20/07 DAY: 1 logging information
range: 12 - 15

High Temp: 94
High Temp: 92
High Temp: 96
High Temp: 97

and so on...

Can you give me tip to get me started?

Also, can you explain these in your script?
1. I know what %1 means but what's the difference between %~1 and %1?
2. What does setting the local variables do for this script?
SETLOCAL ENABLEEXTENSIONS
SETLOCAL ENABLEDELAYEDEXPANSION

Thanks,

jeb
Expert
Posts: 1055
Joined: 30 Aug 2007 08:05
Location: Germany, Bochum

#5 Post by jeb » 08 Oct 2007 15:16

Hi BatchIt,

many questions ...

1. What do the following do?
set filename=!filename:/=-!

!variable! is nearly the same as %variable%, but it will be expanded in the
moment of processing not in the moment of reading the line.
This mechanism must be activated by
SETLOCAL ENABLEDELAYEDEXPANSION
else the "!" has no special effect.

Test this:
SETLOCAL ENABLEDELAYEDEXPANSION
set test=First
for %%a in ("1") do (
set test=second
echo %test% & REM Displays First
echo !test! & REM Displays second
)

the part ":/=-" is for substitution, general %var:old=new% will replace all "old" parts in %var% with "new".
I replace all "/" with "-", because "/" is not allowed in a filename.

2. The difference of %1 and %~1 is only that %~1 will remove one trailing and ending " of an variable, if one or more exist.

example
test.bat
@echo - %1 / %~1

test.bat hello
- hello / hello

test.bat "hello"
- "hello" / hello

test.bat ""hello""
- ""hello"" / "hello"

3. if you want to split your logging file into different subfiles by keywords
you should look at FOR /F "tokens=...
Test the first token for the keyword and redirect the output into
different files.

Hope it helps
jeb

BatchIt
Posts: 6
Joined: 03 Oct 2007 15:04

#6 Post by BatchIt » 09 Oct 2007 00:25

Thx Jeb.

I made several attempts to achieve my goal. My latest version is below.

I am not able to increment my counter variable so I can use it to exist my for loop. I am sure there is an easier way than what I have so far. How can I make this better/simpler?

@echo off

SETLOCAL ENABLEEXTENSIONS
SETLOCAL ENABLEDELAYEDEXPANSION
set filename=nothing
set /a counter=1

for /f "tokens=*" %%a IN (%~1) DO (
call :parseDay "%%a" filename

call :parseTemp

)

goto :eof


::::::::::::::::::::::
:parseDay
for /f "tokens=1,2" %%a in (%1) do (
if "%%b"=="DAY:" (
set filename=%%a.day
set filename=!filename:/=-!
)
)
goto :eof

:parseTemp
for /F "tokens=1,2,3*" %%a in ('"findstr /C:"Temperature:" Temp.txt"') do (
set counter=+1
if %counter%==5 goto :back
echo %%a %%b >> !filename!
)
goto :eof

jeb
Expert
Posts: 1055
Joined: 30 Aug 2007 08:05
Location: Germany, Bochum

#7 Post by jeb » 09 Oct 2007 04:32

Hi BatchIt,

try this, it will seperate your log-infos into different files.


@echo off

SETLOCAL ENABLEEXTENSIONS
SETLOCAL ENABLEDELAYEDEXPANSION
set filename=nothing
set /a counter=1

for /f "tokens=*" %%a IN (%~1) DO (
call :parseDay "%%a" filename

call :splitLog "%%a" !filename!

)

goto :eof


::::::::::::::::::::::
:: Parse a line, if the second word is "DAY" the first word will set the new Filename
:: in the filename all "/" are replaced by "-", so it will be a legal filename
:parseDay "<line>" <filename-var>
for /f "tokens=1,2" %%a in (%1) do (
if "%%b"=="DAY:" (
set "filename=%%a"
set filename=!filename:/=-!
)
)
goto :eof

::::::::::::::::::::::
:: Test if a line begin with a special word "Temperature:", "Low Temp:" or "High Temp"
:: Send the content of the line in seperate files with filename as the base name
:splitLog "<line>" <filename_base>
for /f "tokens=1 delims=:" %%a in (%1) do (
if "%%a"=="Temperature" echo %~1 >> %2.temp
if "%%a"=="Low Temp" echo %~1 >> %2.lowTemp
if "%%a"=="High Temp" echo %~1 >> %2.highTemp
)
goto :eof


have a nice day
jeb

BatchIt
Posts: 6
Joined: 03 Oct 2007 15:04

#8 Post by BatchIt » 10 Oct 2007 04:07

Thx Jeb,

For the variable counter=1,
How do I increment it in a loop?

Also where would I find examples of using arithmetic and boolean expressions?

Do have a terrific day also!

BatchIt
Posts: 6
Joined: 03 Oct 2007 15:04

#9 Post by BatchIt » 10 Oct 2007 10:22

Hi Jeb,

I got the counter variable to increment.

Thx again for helping out.

Post Reply