How Do I Extract Multiple Data and Put Them into CSV Format?
October 19, 2010 8:35 PM Subscribe
Extract
Multiple Data (email, names, dates, and IDs) from a Huge Messy Text File, and Sort Them into a CSV: How do I do this? I've read this on extracting
emails, but I can't figure out how to do this with several different data points.
To flesh things out a bit, here's an example of the text I'm starting with:
______
blah blah blah blah blah blahblah blah blah blah blah blah blah blah blahblah blah blah
blah blah blah blah blah blahblah blah blah
Date: 2007-03-26
Order: 09093SFB
Price: $1.01
Item: blahsomenumberblah
Title: some_item_title_text_string
Name1: JANE
Name2: SMITH
Ctry: US
State: MA
Postl: 10101
Email: blahblahblah@yahoo.com
blah blah blah blah
blah blah
blah blah blah blah blah blahblah blah blah
________________
I'm trying to sort about 2000 pages of data presently in the format above, putting the important stuff into these fields, within csv format:
EMAIL | FIRSTNAME | LASTNAME | TITLE | DATE
Everything but that which fits into the columns above can be discarded.
I've been toying with WinGrep and Notepad++, but to little avail. Any suggestions you could offer would be great. Thanks!
posted by darth_tedious to computers & internet (19 answers total) 5 users marked this as a favorite
Sub AskMe()
Dim sOut As String
Open "C:\testdata.txt" For Input As 1
Open "C:\output.csv" For Output As 2
While Not EOF(1)
Line Input #1, strThisLine
strThisLine = Trim(strThisLine)
If Left(strThisLine, 4) = "Date" Then sDate = Right(strThisLine, Len(strThisLine) - 6)
If Left(strThisLine, 5) = "Title" Then sTitle = Right(strThisLine, Len(strThisLine) - 7)
If Left(strThisLine, 5) = "Name1" Then sName1 = Right(strThisLine, Len(strThisLine) - 7)
If Left(strThisLine, 5) = "Name2" Then sName2 = Right(strThisLine, Len(strThisLine) - 7)
If Left(strThisLine, 5) = "Email" Then
sEmail = Right(strThisLine, Len(strThisLine) - 7)
Print #2, sEmail & ", " & sName1 & ", " & sName2 & ", " & sTitle & ", " & sDate
End If
Wend
Close 2
Close 1
End Sub
Chuck that in the VB editor in excel, fix the input and output filenames, and run.
posted by pompomtom at 9:09 PM on October 19, 2010 [2 favorites]