Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Sorting a fixed length text file..

Status
Not open for further replies.

UC

Programmer
Oct 19, 2001
4
US
I am getting a 250,000 line text file, and need to sort it, and place it into seperate text files according to like item numbers. Here's the catch - it's a fixed length file, with some odd spacing in it.

For instance...

1123foo 1 data...
1121foo 4 data...
1111foo 13 data...
1234foo 121 data...

I would need all '*foo 34' lines to end up in a foo34.txt file, as would the other foo* #'s need to be in the same name formatted files.

Notice the spacing issue, where the spaces after foo decrease as the number increases.

I *could* remove the spaces, but the lines need to remain a fixed length.

So, what I am losing sleep over is this:
I need to sort the text file into seperate text files with the like item #'s grouped together.

Any ideas??

 
Is this something you only need to do once, or something you'll need to be able to run all the time from a web page? If you're only doing it once you could take the easy way and open it up in MS Excel and then Access. They will give you a lot of flexibility on how it formats it and breaks apart the data. Then when you have it in a database just go from there (or keep it in a db instead of all those text files).

If you have to do this web-based and all the time, I'd say there are a few ways to do it -- but here's my two cents. If each line has the same number of characters until the word "data" then the number of spaces isn't such a big deal. You know that the first 4 are the initial number, then the next 3 are "foo," then the next 6 represent the number (you could Trim() that bit to get rid of the spaces). Then you would have the number you need to write that line to the correct file. Just keep reading and appending to the correct files and you should make it okay.

Hope that helps!
 
But, the problem is it's got to remain a fixed length file. If I trim the spaces, I end up with a non fixed length file.

It's something that's actually done over and over again... I'm actually using a compiled php script for it.

 
Well you'll have two variables, the original one with the fixed length and the working one you have trimmed. You could use the trimmed one to figure out where to put the line, but then write the original line to the file.

Would that work?
 
hmm.... not a bad concept. Worth a shot.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top