PDA

View Full Version : help me manipulate text in BAT file



johnny_quest
03-12-2007, 06:20 PM
I have TXT files that have many lines of entries saved from a windows event logger. The lines end with the following....



..... Start Series ODL_RESenddd_CNTR = 15
..... end Series ODL_RESenddd_CNTR = 15
..... Start Series ODL_RESenddd_CNTR = 2
..... end Series ODL_RESenddd_CNTR = 2
..... Start Series ODL_RESenddd_CNTR = 0
..... end Series ODL_RESenddd_CNTR = 0


What I would like to do is create a simple .BAT file that would look at each TXT file line by line and delete each line that doesn't have a number at the end bigger than, say, 12.

in plain english it would be something like...

look at "ODL_RESenddd_CNTR = x"
if x < 12 delete the line

The text files I start with are hundreds of lines long and rarely if ever have the special lines I'm looking for. It would save me so much time if I didn't have to look line by line for the numbers...I'd like to click on this script and have the file filled with only the special lines I'm looking for.

I'm ok at debugging/editing scripts, but not really in the field of making them. What do you think?

johnny_quest
03-14-2007, 09:39 PM
Ok I got a script going that does exactly what I needed... this one deletes anylines that end in "0"... however, I can't figure out how to also make it delete 1, 2, 3.... upto 12.

I tried loops, conditional statements, etc. The fact that the number is in string form makes it tough. I tried to parse it as an integer but I got nowhere.

Somehow I need to stick in <12 in there instead of a variable


Const FOR_READING = 1
Const FOR_WRITING = 2
strFileName = "C:\Documents and Settings\john\Desktop\l.txt"
strCheckForString0 = UCase("0")


Set objFS = CreateObject("Scripting.FileSystemObject")
Set objTS = objFS.OpenTextFile(strFileName, FOR_READING)
strContents = objTS.ReadAll
objTS.Close

arrLines = Split(strContents, vbNewLine)
Set objTS = objFS.OpenTextFile(strFileName, FOR_WRITING)

For Each strLine In arrLines
If Not(Right(UCase(LTrim(strLine)),Len(strCheckForStr ing0)) = strCheckForString0) Then
objTS.WriteLine strLine
End If
Next

sburtchin
03-15-2007, 04:58 PM
Set objTS = objFS.OpenTextFile(strFileName, FOR_READING)I thought you wanted a simple BAT file? WTF is that - Microsoft something? If you like doing these things, hardly an operating system exists that won't do this better than DOS or Windows. I wonder if M$ has ever released a decent byte of code in their entire miserable existence. That said, if you want (really easy) precise control with strings, then COBOL works great, or C for absolute control. For these kinds of things though, operating system commands are almost always best IMHO. Try this:


FILTERIT.BAT MYFILE.TXT MYNEWFILE.TXT
----------------------------------------------
REM NOTE: following is "= ", not just "=" - !
SET prefix==
SET /A count = 12

FIND /N "%prefix%%count%" %1 > %2

:START
SET /A count = count + 1
FIND /N "%prefix%%count%" %1 >> %2
IF NOT '%count%'=='200' GOTO START

This will produce a few duplicates, so you might want to do a SORT afterwards. Please post back how you solve your problem and your final code.;)

johnny_quest
03-16-2007, 02:17 PM
your script turned a 5 line file into a hundred lines that is harder to read than the original.

bash ms all you want at least there script works :)

sburtchin
03-17-2007, 03:53 AM
your script turned a 5 line file into a hundred lines that is harder to read than the original.

bash ms all you want at least there script works :)I had not even tested it, but the script I gave you should turn any 5 line file into nearly 400 (or even more) lines. My intent was not to give you a fully debugged gamma version script. I just thought I could help get you started by providing the essential piece of code to build your script around. The script I gave you does exactly what I had hoped it would do!

Here is my revised script, only slightly modified, which easily turns a 400 line file into a 5 line file (depending on the content, of course):


@ECHO OFF

REM NOTE: following is "= ", not just "=" - !
SET prefix==
SET /A count = 12

FIND /N "%prefix%%count%" %1 > junk.tmp

:START
SET /A count = count + 1
FIND /N "%prefix%%count%" %1 >> junk.tmp
IF NOT '%count%'=='225' GOTO START

FIND "[" junk.tmp | SORT /O %2

DEL junk.tmp

Mine is a Microsoft script too! I think my "WTF is that" was referring to Visual BASIC code. I was hoping you would confirm that. It doesn't look very basic to me. Like everything else Microsoft touches, it looks way more complicated than it has to. I spent a couple hours trying to learn Visual BASIC because I heard other guys talking about using it a lot. I thought "Jeez! - Why not just write it in C, and save time typing." Is there anything this does that C wouldn't do better:confused:? My script produces a few duplicate records, and yours looks complicated to me. I stand by my Microsoft bashing.

Did you get your script working? Please post it.

sburtchin
03-17-2007, 02:17 PM
One final tweak - it is sufficient to stop searching when count equals "119" (ie. '%count%'=='119'). This has the advantage of: 1) cutting execution time by 50%, and 2) eliminating duplicate records.


What I would like to do is create a simple .BAT fileYou did say you wanted a .BAT file:).