Solved How to find a string in data/text file under a header.

October 9, 2013 at 09:02:20
Specs: Windows 7
For example here is the data from the file.

 input_data_size_cel input_data_size_byt aggregate_data_size aggregate_data_size kernel_queries_trac total_query_cost    query_tracking_enab incremental_data_sl incremental_input_d incremental_aggrega
           795200559         14479720448          1908143354         34516402176                   0                   0               FALSE                   1               34360                   0

I need to capture/copy string under "input_data_size_byt" and "aggregate_data_size" from 2 files created at 9 AM and another at 9 PM.

I would prefer VB script or DOS batch script.

edited by moderator: Added pre tags -Razor2.3

See More: How to find a string in data/text file under a header.

Report •

October 9, 2013 at 13:26:24
✔ Best Answer
Const ForReading = 1

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objTextFile = objFSO.OpenTextFile("c:\testfile.txt", ForReading)

For i = 1 to 2

strNumber = objTextFile.ReadLine
'Wscript.Echo strNumber

NumbersArray = Split(strNumber)
'For Each strNumber in NumbersArray
'   wscript.echo strNumber 
iDs = NumbersArray(1)
aDsOne = NumbersArray(2)
aDsTwo = NumbersArray(3)

Wscript.Echo "input_data_size_byt   " & iDs & "   |aggregate_data_size   " & aDsOne & "   " & aDsTwo


might be ugly, but works on that data set assuming there is three lines in the file.


Report •

October 9, 2013 at 19:35:29
Thank you Mike for the solution. I posted only 3 lines so it would be easier to explain. It is part of bigger file.
How to use the code for any string under any header?

Report •

October 9, 2013 at 20:38:48
Are you intending then to put in a search option. It looked as though you wanted three specific numbers. How is the file deliminated? My concern is that you seem to want numbers related to headers, but based on this setup, there is a good chance that things won't line up right. Perhaps re-posting the data file with a " pre tag in front, and a pre tag at the end.

message edited by mikelinus

Report •

Related Solutions

October 9, 2013 at 20:52:14
Here's a "sloppy seconds" batch rendition:
::------- start batchscript
@echo off & setlocal
set tag=input_data_size_cel input_data_size_byt aggregate_data_size aggregate_data_size kernel
for /f "tokens=1 delims=[]" %%a in ('find /n /i "%tag%" ^<file1.txt') do (
set sk=%%a
goto :xx
for /f "tokens=2,3 skip=%sk%" %%b in ('find /v "-----" ^<file1.txt') do echo %%b,%%c
::------- end batch

Report •

Ask Question