Microsoft > Microsoft DOS

Finding multiple strings in a text file

<< < (3/4) > >>

Freerefill:
I've attached the file I'm searching through, and here's my code as it stands:

@echo off
REM Clear existing files
if exist C:\fr1.txt del C:\fr1.txt
if exist C:\fr2.txt del C:\fr2.txt
if exist C:\fr3.txt del C:\fr3.txt
if exist C:\fr4.txt del C:\fr4.txt
if exist C:\fr1a.txt del C:\fr1a.txt
if exist C:\fr2a.txt del C:\fr2a.txt
if exist C:\fr3a.txt del C:\fr3a.txt
if exist C:\fr4a.txt del C:\fr4a.txt
if exist C:\for1.txt del C:\for1.txt
if exist C:\for2.txt del C:\for2.txt
if exist C:\for3.txt del C:\for3.txt
setlocal enabledelayedexpansion
set success=0

REM Export selected data to files
findstr /I /N /X "{ACAD_REACTORS" C:\dwg.txt > C:\fr1.txt
findstr /I /N /X "330" C:\dwg.txt > C:\fr2.txt
findstr /I /N /X "C" C:\dwg.txt > C:\fr3.txt
findstr /I /N /X "102" C:\dwg.txt > C:\fr4.txt

REM Obtain the first number from the first file. May need a loop.
for /f "eol= tokens=1 delims=:" %%i in (C:\fr1.txt) do @echo ^%%i>>C:\fr1a.txt
for /f "eol= tokens=1 delims=:" %%i in (C:\fr2.txt) do @echo ^%%i>>C:\fr2a.txt
for /f "eol= tokens=1 delims=:" %%i in (C:\fr3.txt) do @echo ^%%i>>C:\fr3a.txt
for /f "eol= tokens=1 delims=:" %%i in (C:\fr4.txt) do @echo ^%%i>>C:\fr4a.txt

REM Grab the first string in the first file
for /f "eol= tokens=1 delims=" %%a in (c:\fr1a.txt) do (
set /A var1=%%a+3
for /f "eol= tokens=1 delims=" %%b in (c:\fr2a.txt) do (
set /A var2=%%b+2
if "!var1!"=="!var2!" echo !var1!>> c:\for1.txt
)
)

for /f "eol= tokens=1 delims=" %%a in (c:\fr3a.txt) do (
set /A var3=%%a+1
for /f "eol= tokens=1 delims=" %%b in (c:\for1.txt) do (
set /A var4=%%b
if "!var3!"=="!var4!" echo !var3!>> c:\for2.txt
)
)

for /f "eol= tokens=1 delims=" %%a in (c:\fr4a.txt) do (
set /A var5=%%a
for /f "eol= tokens=1 delims=" %%b in (c:\for2.txt) do (
set /A var6=%%b
if "!var5!"=="!var6!" echo !var5!>> c:\for3.txt
)
)

pause

... set up to run as a .bat file. Running it yields the following, in the file C:\for3.txt:

1905
1921
1937
1965
1993
2017
2037
2053
2077
2225
2245
2325
2345

Which is the line number for every instance of the string "102" which immediately follows the strings "{ACAD_REACTORS", "330" and "C"

[attachment deleted by admin]

Dias de verano:
So what is it that you want to happen? You want to know the location of the first occurence of the line following your 4 line sequence? Or the location of all of them?

is it the case that the 4 line sequence will only occur once in a dwg file?


Freerefill:
In a nutshell, I'm trying to find all of them. Which is what the code is currently doing.

What I want to do is expand the pattern to more strings, from 4 to possibly upwards of 10 or more, because that will narrow down the search, and if I'm correct, yield one single instance of a pattern, yet to be determined, from which I can gather the data I really need, which I'm sure I can do on my own.

If you look through the attached file, search for the string "Layout1". It will appear twice within the file. The string represents a facet in a drawing program, a layout tab to be precise (those of you who have worked with AutoCAD will understand this better). You'll also find, near it, a "Layout2" and a "Model". Those bits of data are what I ultimately want to find. If you look just before the first set in which these three bits of data appear, you'll see the 4 lines that my code currently searches for.

As I said, "Layout1" will not always be "Layout1". It could be "Sheet1" or "Tab1" or "Pecan Butter". It can be anything. Hence the need to find something that IS common.

If I'm still confusing you, let me know... but I don't know how else I can describe it...

gh0std0g74:
here's a vbscript

--- Code: ---Set objFS = CreateObject("Scripting.FileSystemObject")
strFile = "c:\test\file.txt"
Set objFile = objFS.OpenTextFile(strFile)
Dim first
Dim sec
Dim third
first=""
sec =""
third=""
i=0 'line count
Do Until objFile.AtEndOfStream
i=i+1
strLine = objFile.ReadLine
If strLine = "102" Then
If third = "{ACAD_REACTORS" And sec="330" And first="C" Then
WScript.Echo "line number: " , i
WScript.Echo "third: " ,third
WScript.Echo "sec: " ,sec
WScript.Echo "first: " ,first
End If
End If
third = sec
sec = first
first=strLine
Loop


--- End code ---

output:

--- Code: ---C:\test>cscript /nologo test.vbs |more
line number:  1905
third:  {ACAD_REACTORS
sec:  330
first:  C
line number:  1921
third:  {ACAD_REACTORS
sec:  330
first:  C
line number:  1937
third:  {ACAD_REACTORS
sec:  330
first:  C
line number:  1965
third:  {ACAD_REACTORS
sec:  330
first:  C
line number:  1993
third:  {ACAD_REACTORS
sec:  330
first:  C
line number:  2017
third:  {ACAD_REACTORS
sec:  330
first:  C
line number:  2037
third:  {ACAD_REACTORS
sec:  330
first:  C
line number:  2053
third:  {ACAD_REACTORS
sec:  330
first:  C
line number:  2077
third:  {ACAD_REACTORS
sec:  330
first:  C
line number:  2225
third:  {ACAD_REACTORS
sec:  330
first:  C
line number:  2245
third:  {ACAD_REACTORS
sec:  330
first:  C
line number:  2325
third:  {ACAD_REACTORS
sec:  330
first:  C
line number:  2345
third:  {ACAD_REACTORS
sec:  330
first:  C

--- End code ---

Freerefill:
Thank you.. but I was really looking for a .bat file..

I'm well aware of the inefficiencies, but since I'm still new to this, I have no clue how to improve. I did the best I could with what I had, and since I still don't even know exactly how a "token" works, I had to produce some sort of visual output to make sure I was on the right track. Hence the multiple output files.

All that said.. if there are so many problems with my code.. how do I fix them? And no, I'm not looking for a quick fix, I want to know the methods and implement them myself.. I do want to learn.

Navigation

[0] Message Index

[#] Next page

[*] Previous page

Go to full version