Batch file search pivot table

Windows 2000
December 28, 2009 at 01:52:27
Specs: Windows XP
My primary search is for a unique value in column 3 which appears under one heading. Once located I would need a type of "backwards" search to find the heading that the primary value was located under so I could add that to a secondary variable. I am struggling with what the best way to approach this might be..

col1-row1 col2-row1 col3-row1
col1-row2 col2-row2 col3-row2

col1-row1 col2-row1 col3-row1
col1-row2 col2-row2 col3-row2

See More: Batch file search pivot table

Report •

December 28, 2009 at 01:58:41
NOTE: I am using batch files and the file I am searching is a text file. Additional headings are occasionally added overtime that contain "sub-records."

Report •

December 28, 2009 at 06:03:29
Is it fairly accurate to say that the heading is the first line in each block, if we define blocks to be separated by blank lines?

Helping others achieve escape felicity


Report •

December 28, 2009 at 07:02:18
Yes; however, I must find the value in the 3rd column first and
then "backtrack" to obtain the appropriate section name.

Report •

Related Solutions

December 28, 2009 at 12:24:30
Just a suggestion, You can try like save the heading to a

Then search for the unique value in col3... if you get that...just
use the heading what you have saved... so that we can avoid

i believe this can be done... I know there are lots of experts
here who can make it possible...

I too will try my best...

Report •

December 28, 2009 at 13:36:58
@IIavvk exactly! that was my approach here, anyhow:

@echo off & setlocal
set /p targ=data to find (column 3)
for /f %%z in ('find /v /c "" ^< test2') do set lc=%%z
echo %lc%
set xx=
set cc=0
call :one < test2

set /a cc+=1
if %cc% gtr %lc% goto :eof
:echo %cc% %lc%
set prev=%xx%
set xx=
set /p xx=
:echo [%xx%]
if "%prev%" equ "" (
set hdr=%xx%
goto :one
for /f "tokens=1-3*" %%a in ("%xx%") do (
if [%%c] equ [%targ%] (
echo found: header %hdr% data %%c equals target %targ%
goto :one

if the header has some distinguishing characteristic, it would save a lot of time instead of using the blank lines, because the whole file has to be read through regardless of where it finds the target (the way i've set it up here, there is another way however). also, don't know what you want to do with the data once you've captured it.

Report •

December 29, 2009 at 01:29:32
Users run the batch and type in an unfamiliar zipcode given to them by another user. So they type in 12345 and the data returned to the screen should be the county name "Washington" and "farmville 1214 12345". So the match is returning the entire record as well as the county name back to the screen. Thank you @nbrane and @IIavvk. I am trying to work the information you have given me.

city population zipcode
city population zipcode

farmville 1214 12345
mafiawarville 5610 11234
fishville 3512 11123

city population zipcode
city population zipcode
city population zipcode

Report •

December 29, 2009 at 08:04:13
here's another approach that only reads the file once (storing it in a set of variables) and allows a "direct lookup" of the zip.
this one "assumes" that no countyname has two spaces
in it (three "tokens" or words)
another critical assumption is that there's only one incidence of any given zipcode in the file. it could be written to handle more than one, but that gets more involved.
@echo off & setlocal enabledelayedexpansion
:: read in the data
for /f "tokens=1-3 delims= " %%a in (test2) do (
if "%%c" equ "" (
set cty=%%a %%b
) else (
set xx=b%%c
set !xx!=county: !cty! city: %%a pop: %%b zip: %%c
set b=goodbye
:: allow query of zipcodes until null entry
set xx=
set /p xx=zip:
set data=b!xx!
if not defined !data! (
echo zip not found
) else (
echo !%data%!
if "!xx!" neq "" goto :one

Report •

December 29, 2009 at 13:29:59
Hi You can try the below script too..

@echo off
set /p zip=Enter the ZIP:
setlocal enableextensions enabledelayedexpansion

set "i=1"

for /f "delims=" %%l in (details.txt) do (

if !i! LSS 5 (

echo line!i!:"%%l" >> d1.txt
set h!i!=%%l

set /a i=!i!+1

if "!i!"=="5" (
set "i=1"

rem pause

for /f "tokens=* delims=" %%n in ('findstr "%zip%" d1.txt') do
echo %%n >>nn.txt
if exist nn.txt (goto eo)



if exist nn.txt (
echo %h1%
del /q nn.txt
) else (
echo No Match Found!!!

if exist d1.txt (del /q d1.txt)

set /p Q=Would you like to Search again (Y/N)?

if /I "%q%"=="y" (
goto startover


Note: I have written the script considering same number of
records under each country. In this case i have taken 3
records per each country

Report •

Ask Question