Find a string and then search upwards

July 8, 2010 at 15:35:12
Specs: Windows XP
Hi Guys. Can anyone help me with a batc issue. Suspose I have the following

LINE of Info XX Bla bla
INFO
INFO
INFO
Line of Info XX Blaa
INfo
Info <-- I want this record
Info

So the Final out look will be ;

Line of Info XX Blaa
Info

The findstr will return the "info" section, but I need a script that will serach upwards the the previous instasnce of the Line of Info XX Blaa

The Line of Info XX Blaa will start with ENV but most of the rest of stuff on that line is unique.
I would like to use a batch as thats how Im searching for the "info" which works perfect.

The file is a .txt dump, dut thanks the the boys on my previous I have been able to insert line to seperate the records which greatly helps the findstr.

Also an updated recorded may appear, so I would need a loop to search the for each INFO it finds. Any ideas ?


See More: Find a string and then search upwards

Report •


#1
July 9, 2010 at 05:27:45
Let's imagine that the sample data you provided more closely
resembles your actual data...

ENV some info Bla bla
INFO
INFO
INFO
ENV some info Bla bla
INFO
INFO
INFO
ENV some info Blaa
INfo
InfoX
Info

...where 'InfoX' is the record you're interested in and
'ENV some info Blaa' is the line that you want before it.
Try the following:

@echo off

setlocal EnableDelayedExpansion

for /f "delims=" %%a in (dump.txt) do (
  echo %%a| findstr /r /c:"^ENV"> nul
  if !errorlevel! neq 1 (
    set var1=%%a
  ) else (
    echo %%a| findstr "InfoX"> nul
    if !errorlevel! neq 1 (
      set var2=%%a
      echo.!var1!
      echo.!var2!
    )
  )
)



Report •

#2
July 9, 2010 at 05:51:21
Hi
The above layout is all in an actuall txt file. Basicly there are records in the txt file, and I need to pull the individual records and the ENV line, rather than all the INFO and the ENV line

The info line spans about 20 - 50 lines of text.
All text is unformated


Report •

#3
July 9, 2010 at 06:53:14
You need to be more clear about what you want. This,
for example:

The info line spans about 20 - 50 lines of text.

How can a line span 20-50 lines of text?

Are you saying that

Info <-- I want this record

marks the end of a record and you want this line and
all other lines going back to 'ENV some info Blaa',
inclusive?


Report •

Related Solutions


Ask Question