Search a CSV File for a Certain String? Topic is solved

Get help with using AutoHotkey and its commands and hotkeys
MaxAstro
Posts: 552
Joined: 05 Oct 2016, 13:00

Search a CSV File for a Certain String?

01 Mar 2017, 16:19

I am looking for a way to extract every instance of a string in a certain format from a CSV file. Specifically, I am looking for barcodes, in the format #-#####-#####-#. However, sometimes the barcodes are by themselves and sometimes they are part of a larger string; there's no real pattern to the layout of the CSV file because the person who sends it to me is... frustrating to work with. :P

I'm guessing what I need is a combination of a file read loop and a RegEx, but A) I'm really bad at RegEx, and B) I don't really know how a file read loop works with a CSV file.

The end goal would be for the script to grab each barcode and append it to a new file on its own line, so that by the end there is a file containing just the barcodes in a vertical column.
TravisQ
Posts: 15
Joined: 17 May 2015, 23:51

Re: Search a CSV File for a Certain String?  Topic is solved

01 Mar 2017, 21:02

Maybe something like this could work for you

Code: Select all

FileRead,File,%A_Desktop%\test.txt
Results:=[],p:=1
while p := RegExMatch(File, "\d-\d+-\d+-\d", Match, p + StrLen(Match)) {
    Results.push(Match)
}
for k,v in Results
append.=v "`n"
FileAppend,%append%,%A_Desktop%\test2.txt
MaxAstro
Posts: 552
Joined: 05 Oct 2016, 13:00

Re: Search a CSV File for a Certain String?

02 Mar 2017, 12:03

First off, WOW that is a clever way of doing a multiline append instead of calling FileAppend repeatedly. ~steals~

That does look like what I need. Specifically the "\d-\d+-\d+-\d" bit I doubt I ever could have figured out on my own. Looks like AHK reads in CSV files more or less just fine, grabbing an entire line at once; I can use a Parse loop to split it from there so that should work. Thanks!

Return to “Ask For Help”

Who is online

Users browsing this forum: Bing [Bot], Dysphoria, Google [Bot], Odlanir and 26 guests