Computing.Net > Forums > Programming > Batch file to extract certain data

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Batch file to extract certain data

Reply to Message Icon

Name: jasdjensen
Date: April 13, 2008 at 12:38:00 Pacific
OS: Win XP/2003
CPU/Ram: Any
Product: Any
Comment:

I'd like help creating a batch file that will search a log file for certain sections of text and export them to a log file.

example log file:
blah blah blah Startingword This is the sentence I want to grab. Endingword blah blah blah

output:
This is the sentence I want to grab.


So would want to be able to set the key starting and ending strings and then have it grab any information in the middle and output it to a file.

Thanks!



Sponsored Link
Ads by Google

Response Number 1
Name: ghostdog
Date: April 13, 2008 at 18:54:17 Pacific
Reply:

if you can download Gawk from here:
http://gnuwin32.sourceforge.net/pac...


{
match($0,/Startingword/)
start=RSTART+RLENGTH+1
sub(/Endingword.*/,"")
print substr($0,start)
}

save the above as script.awk and on command line

c:\test> gawk -f script.awk yourfile


0

Response Number 2
Name: jasdjensen
Date: April 13, 2008 at 19:09:01 Pacific
Reply:

Is there a way to do this without installing additional software?


0

Response Number 3
Name: jasdjensen
Date: April 13, 2008 at 19:44:37 Pacific
Reply:

The solution above doesn't work for me. I got the gawk exe and ran the script as:

"C:\Program Files\GnuWin32\bin\gawk.exe" -f script.awk test.txt

with the script containing:
{
match($0,/Rip/)
start=RSTART+RLENGTH+1
sub(/##.*/,"")
print substr($0,start)
}


(I want it to look for "Rip" and follow it to the following "##" and export the string in the middle)


0

Response Number 4
Name: ghostdog
Date: April 13, 2008 at 21:18:18 Pacific
Reply:

use this


{
match($0,/Startingword/)
start=RSTART+RLENGTH+1
gsub(/\#\#.*/,"")
print substr($0,start)
}


0

Response Number 5
Name: ghostdog
Date: April 13, 2008 at 21:29:40 Pacific
Reply:

If you are not comfortable still, you can use vbscript


Set objFS=CreateObject("Scripting.FileSystemObject")
strMyFile = "c:\test\test.txt"
Set objFile = objFS.OpenTextFile(strMyFile)
Do Until objFile.AtEndOfLine
strLine = objFile.ReadLine
startPos = InStr(1,strLine,"Startingword")
If startPos > 0 Then
strLine = Mid(strLine,startPos+len("Startingword"))
endPos = InStr(startPos,strLine,"##")
strLine = Mid(strLine,1,endPos-1)
WScript.Echo strLine
End If
Loop


0

Related Posts

See More



Response Number 6
Name: jasdjensen
Date: April 13, 2008 at 21:33:03 Pacific
Reply:

It's still not working.

Maybe I'm not being clear and that's what the problem is.

Example log:
blah blah
4/10/07 07:50 Rip - blah blah really long sentence
more blah blah
more stuff ##
4/11/07 10:00 blah blah

output sent to file should be:

4/10/07 07:50 Rip - blah blah really long sentence
more blah blah
more stuff ##


0

Response Number 7
Name: ghostdog
Date: April 13, 2008 at 23:37:21 Pacific
Reply:

try this


/Rip/,/\#\#$/{
print $0
}


0

Response Number 8
Name: jasdjensen
Date: April 14, 2008 at 13:32:48 Pacific
Reply:

ok.. the solution below works:

/Rip/,/\#\#$/{
print $0
}

Its what I needed. Thanks! I really appreciate it.


0

Sponsored Link
Ads by Google
Reply to Message Icon






Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: Batch file to extract certain data

Create a Batch file to extract data www.computing.net/answers/programming/create-a-batch-file-to-extract-data/20346.html

Batch file to extract certain lines www.computing.net/answers/programming/batch-file-to-extract-certain-lines/17465.html

Batch file to extract data from ini www.computing.net/answers/programming/batch-file-to-extract-data-from-ini/12995.html