Finding strings and outputting them to file

December 11, 2009 at 18:10:56
Specs: Windows Vista
Hi, I've got a folder full of .log files that contain
among many other things this string that I'm
interested in:
<ABC:xxxxx>

ABC is constant, and the x's are variable

So, I want to search through through a folder
full of .log files, find the string ABC:xxxxx and
output each instance of this string, without the
< or > to a file.

I'd appreciate any help, thanks!


See More: Finding strings and outputting them to file

Report •


#1
December 11, 2009 at 18:29:51
so how does your input and output file looks like? provide a sample of those files

GNU win32 packages | Gawk


Report •

#2
December 11, 2009 at 18:46:22
Here's a sample input file:

"Avree<23><STEAM_0:1:20574508><Blue>" with "bat" (attacker_position "-1905 4639 -471") (victim_position "-1845 4619 -477")
L 09/26/2009 - 18:25:17: "[SS]Velkira<27><STEAM_0:0:14754629><>" entered the game
L 09/26/2009 - 18:25:24: "[SS]Velkira<27><STEAM_0:0:14754629><Unassigned>" joined team "Red"
L 09/26/2009 - 18:25:26: "[SS]Velkira<27><STEAM_0:0:14754629><Red>" changed role to "scout"
L 09/26/2009 - 18:25:29: "M • teh_meetch<26><STEAM_0:1:17459239><Unassigned>" joined team "Blue"

Output:

STEAM_0:1:20574508
STEAM_0:0:14754629
STEAM_0:0:14754629
STEAM_0:0:14754629
STEAM_0:1:17459239


Report •

#3
December 11, 2009 at 19:35:42
this not tested, not very robust (it depends
rather heavily on fairly regular and consistant data format):

@echo off > listfil && setlocal enabledelayedexpansion
for /f "tokens=* delims=" %%a in ('find "><STEAM_0:" *.log ^|find /v "----------"') do (
for /f "tokens=2 delims=>" %%b in ("%%a") do (
set yy=%%b
echo !yy:~1! >> listfil)
)


Report •

Related Solutions

#4
December 11, 2009 at 22:28:28
It is fairly uniform, though the number of numbers after the 0:0 or
0:1 is variable to some small extent. Testing your script now,
thank you!

Report •

#5
December 11, 2009 at 22:52:32
Okay, seems to be working fine, just getting a few instances
of unwanted strings but it's not a big deal and I can just delete
them easily.

For instance, if you have this string:
<STEAM_0:0:27529676><Red>
It'll show "Red" in listfil as well

Also, it's a bit slow with a lot of files but that's to be expected
since I'm sorting through a few hundred files.

Thanks again!


Report •

#6
December 11, 2009 at 23:40:33
do yourself a favour, for parsing HTML or text/file, use a proper text/HTML parsing tool. You can program in Perl,Python where there are HTML parsing modules for you to use. If not, at least learn to use something decent at parsing text, such as vbscript, or the venerable gawk (see my sig). These tools are designed for that purpose. Here's the one liner that does the job. Forget about using batch for such stuff...
C:\test>gawk -vRS=">" "/STEAM/{ sub(/</,\"\") ; print}" file
STEAM_0:1:20574508
STEAM_0:0:14754629
STEAM_0:0:14754629
STEAM_0:0:14754629
STEAM_0:1:17459239

GNU win32 packages | Gawk


Report •

#7
December 12, 2009 at 00:20:01
ghostdog: i agree, batch is usually just for a "quick hack", but sometimes that might be ok IF you (ie: enduser) are aware of limitations and quirks and just want quick fix, of simple problem. Believe me, anything critical requires very high scrutiny!
I believe most software is prone to failure if you press it hard enough, and/or op.systems evolve beyond it, (which will be indefinitely until the post-apocolyptic period!)
There are literally millions of tools out there, all of them excellent for their domain (time, op.sys, hardware). I am just now exploring the tip of the gnu iceberg, among others.
I still use (and I'm embarassed to admit it) quickbasic, when
it suits my needs and gets the job done. (not much anymore though... vb 3.2 has almost identical coding syntax for sloppy programmers like me) (also, qb was pre winXp, before batchscript enhancements.) anyway: let the "buyer" (or in this case, "poster") beware: what I put up is ALWAYS only a hack, not an application, user interface, or universal utility.
I typically write and suffer under my own parsing algorithms,
but at least when they fail, I know where to look. (ok, sometimes!) And parsing is, imho, the most siginicant and difficult of programming. apologies long post... been a long night...

Report •

#8
December 12, 2009 at 00:55:19
to broseph: yes, sorry, i looked at your sample and saw where I slopped out (lazy approach). will try using "_" based on assumption that the first "_" will delimit the target base:

for /f "tokens=1,2* delims=_"" %%b in ("%%a") do (
for /f "tokens="1 delims=>" %%c in ("%%b") do (
set out=STEAM_%%c
echo !out! >> outfil)
)

as ghostdog pointed out, this is all just hackwork and not elegant coding. since this patch not tested, i know it will fail, but when i get some z's and some coffe i'll see what develops.


Report •

#9
December 12, 2009 at 10:22:36
It really isn't that critical so I'm happy with a hack as you put
it. Also, what do I need to change to that second batch code posted to adapt it to reading all the .log files in a folder?

Regarding gawk, is this the kind of thing I need to use in
command line?

C:\logs\*.log>gawk -vRS=">" "/STEAM/{ sub(/</,\"\") ; print}"
idlist.txt

I need to look at the documentation but if you knew it off hand
it'd save me a lot of time.

The only programming language I know is vb6 so I could use
that but I figured it'd be much slower than other approaches.

Thanks again!


Report •

#10
December 12, 2009 at 12:16:09
did that work? looking at it today, it looks like it would not have worked (the second version of code). apologies for posting sloppy and untested code. this version is tested against a minimal set of fake log files, and *should* scan all the files in one folder (but not subdirectories).

@echo off > outfil && setlocal enabledelayedexpansion
for /f "tokens=* delims=" %%a in ('find "><STEAM_0:" *.log ^|find /v "----------"') do (
set uu=%%a
call :bbb)
goto :EOF

:bbb
for /f "tokens=1* delims=_" %%b in ("!uu!") do (
set uu=%%c
set xx=%%b
set xx=!xx:~-5!
if "!xx!" equ "STEAM" goto :xxx
)
goto :ex
:xxx
for /f "tokens=1 delims=>" %%e in ("!uu!") do (
set out=!xx!_%%e
echo !out! >> outfil)
:ex


Report •

#11
December 12, 2009 at 22:28:28
>> Regarding gawk, is this the kind of thing I need to use in
command line?

if what you are doing is simple, yes you can create just one liners on the command line. BUT gawk is a small programming language so you can create full fledge programs/scripts with it, not just one liners.

GNU win32 packages | Gawk


Report •

#12
December 13, 2009 at 21:42:58
Works great, thanks!

Report •

#13
December 14, 2009 at 10:42:15
Sorry, to clarify I'm using nbrane's script and it's working great -
thanks a lot!

I'm looking into gawk as well because it probably will be quicker
for the amount of files I'm sorting through.


Report •


Ask Question