Jump to content

Sky Slate Blueberry Blackcurrant Watermelon Strawberry Orange Banana Apple Emerald Chocolate

Possible to parse text file with AHK?


  • Please log in to reply
10 replies to this topic
Parker
  • Guests
  • Last active:
  • Joined: --
Say I have a text file, and the contents are going to be fed into excel using a delimited format. The problem is some of the lines have mixed up columns.

It should read:
A, B, C, D, E, F, G

But some read:
A, F, G, C, D, B, E

Is there a way to turn those "bad" lines/rows into "good" ones?

I've tried RegExMatch and appending it to my file, but as far as I can tell, it'll only do one line/row. I was thinking about setting up a loop, but the number of lines/rows varies between files. Also, I've noticed when it doesn't match something, it'll move to the next row which contains the value, and I can't have that...

Here's the RegExMatch I was using:

XPos := RegExMatch(Revision, "x`;[0-9]+`;",XCoord,1)
yPos := RegExMatch(Revision, "y`;[0-9]+`;",yCoord,1)
AlliancePos := RegExMatch(Revision, "Alliance`;[az-A-Z0-9]+`;",Alliance,1)
NamePos := RegExMatch(Revision, "Name`;[a-zA-Z0-9-_]+`;",Name,1)
MightPos := RegExMatch(Revision, "Might`;[0-9]+`;",Might,1)
FileAppend, `n%Xcoord%%yCoord%%alliance%%Name%%Might%, %filepath%


SoLong&Thx4AllTheFish
  • Members
  • 4999 posts
  • Last active:
  • Joined: 27 May 2007
For CSV I recommend the CSV Library
<!-- m -->http://www.autohotke...pic.php?t=55909<!-- m -->
as it will be the closest thing you'll get to any robust parsing of a CSV file (note the bug fix in the thread at the end as well if you are going to use CSV_MatchCellRow)

And of course I can recommend my TF library for generic text files
<!-- m -->https://ahknet.autoh...ugov/tf-lib.htm<!-- m --> but then again I'm probably biased :wink:

Parker
  • Guests
  • Last active:
  • Joined: --
I have your library installed, hugov, but at first glance, I didn't see anything which would do what I wanted. Do you have and recommendations as to which function will work best for me?

I'll look into the CSV lib, but the file is .txt, and the delimiter is a semi-column, so I'm not sure it'll work for me.

Fill in the Blank
  • Members
  • 29 posts
  • Last active: Jun 13 2011 05:47 PM
  • Joined: 26 Jan 2011
Bump

I would very much like to know this.

Rseding91
  • Members
  • 703 posts
  • Last active: Apr 02 2016 05:05 AM
  • Joined: 07 Jun 2010
You do know excel can import CSV files into the cells based on the commas right?

You could import the CSV and simply move around the wrong sections then save it out again as a CSV.

Or, do you have multiple files and you are trying to automate some regular process?

Fill in the Blank
  • Members
  • 29 posts
  • Last active: Jun 13 2011 05:47 PM
  • Joined: 26 Jan 2011
You guessed it - I have multiple files (1,444 to be exact). Each of the files returns data in a particular order, however, for reasons unbeknown to me, the server randomly returns files with data in a different order.

I can do this in Excel, manually, but it takes a considerable amount of time, especially because there could by anywhere up to four different orders to the data. All in all, there's about 170k lines of data, and manually trying to rearrange the data is a huge pain.

Fill in the Blank
  • Members
  • 29 posts
  • Last active: Jun 13 2011 05:47 PM
  • Joined: 26 Jan 2011
I'll try to explain what I mean, better...

One file might contain information like this:

X:00;Y:00;Name:Joe;Type:a

But another file, which contains the same types of data (same columns) might look like this:

X:00;Type:a;Y:00;Name:Joe

Would like to somehow rearrange those data elements into the expected order. Hopefully I'm articulating this properly... Anyone know if it's possible, and if so, which commands I would need to use?

Rseding91
  • Members
  • 703 posts
  • Last active: Apr 02 2016 05:05 AM
  • Joined: 07 Jun 2010
Do all of the excel files contain the same things?

X, Y, Name and Type ?

Fill in the Blank
  • Members
  • 29 posts
  • Last active: Jun 13 2011 05:47 PM
  • Joined: 26 Jan 2011
They aren't excel files - they are delimited text files (not sure how much of a difference that makes, but wanted to clarify just in case). But to answer your question, yes, they contain all the same columns of data, just in different orders.

These text files were once JSON files pulled from an app server. I've modified them from their typical JSON format to a delimited format:

From:
{"x":00,"y":00,"name":"Joe","type":"a"}
To:
X:00;Y:00;Name:Joe;Type:a

I left their labels in for the purpose of being able to search them in attempts to rearrange the out-of-order data.

  • Guests
  • Last active:
  • Joined: --
data =

(

X:00;Type:a;Y:00;Name:Joe

X:00;Y:00;Name:Joe;Type:a

Name:Joe;X:00;Type:a;Y:00

Type:a;Y:00;X:00;Name:Joe

)

Loop, Parse, data, `n, `r

	NewData .= Format(A_LoopField) "`n"

MsgBox, %NewData%



Format(in) {

	static n := ":([^;]*)"

	RegExMatch(in, "X" n, X)

	RegExMatch(in, "Y" n, Y)

	RegExMatch(in, "Name" n, Name)

	RegExMatch(in, "Type" n, Type)

	return, X ";" Y ";" Name ";" Type

}


Fill in the Blank
  • Members
  • 29 posts
  • Last active: Jun 13 2011 05:47 PM
  • Joined: 26 Jan 2011
Ah, thanks a million! That's exactly what I needed!!