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 Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Unix terminators in PC txt files

Status
Not open for further replies.

phudgens

Technical User
Jul 8, 2004
117
US
Does anyone know how to use VBA code to deal with Unix terminators that come across in ascii files transferred from Unix to PC? I cannot get my code to correctly read individual lines of data in files of this type.

Thanks,
Paul Hudgens
Denver
 



Hi,

Never had this problem.

Please post several rows (copy 'n' paste)

Skip,
[glasses]Don't let the Diatribe...
talk you to death![tongue]

[glasses]Just traded in my old subtlety...
for a NUANCE![tongue]
 
Are you sure the file gets them on a unix to windows transfer?

If the file in the unix environment already has them, you can run the dos2unix script on it.

Otherwise, a find and replace option may help.

[Blue]Blue[/Blue] [Dragon]

If I wasn't Blue, I would just be a Dragon...
 
I use the following code:

Open FileName For Input As #1
Do While Not EOF(1)

Line Input #1, LineString
LineString = Replace(LineString, "~", " ")
LineString = Replace(LineString, vbTab, " ")
LineString = Trim(LineString)

to read the following data:


#-------- -------------- ----- -------------------
DEPT .F :0 Index
DEVI .DEG :1 Hole Deviation
HAZI .DEG :2 Hole Azimuth
~Ascii Data Section
3980.00000 2.12431 63.49890
3980.50000 2.12431 63.49890
3981.00000 2.12431 63.49890
3981.50000 2.12431 63.49890
3982.00000 2.12431 63.49890
3982.50000 2.12437 63.49815
3983.00000 2.12674 63.46420
3983.50000 2.13017 63.39584
3984.00000 2.13136 63.30776
3984.50000 2.13463 63.24054
3985.00000 2.13659 63.08165
3985.50000 2.13746 63.01556
3986.00000 2.13766 62.94527


Due to the Unix termination characters that come across from our Unix server, extremely long lines (>1000 characters) gets read into LineString. My program cannot then read the appropriate keywords to know when to start reading data. If the data file is loaded into a text editor and saved, the problem is solved - somehow doing that gets rid of the Unix terminators.

Thanks,
Paul H.
 
Your mention of dos2unix fired some synapses in a dark corner of my severly atrophied brain and I remembered that there is also a unix2dos command on dos. That command can be invoked from within my VBA code via the Shell command and has solved the problem. Many thanks,

Paul Hudgens
Denver
 
DOS (Windows) and Unix have different line terminators. MS uses CRLF, Unix uses LF only. Which is why Windows thinks you have really long lines in your file with strange characters where the linefeeds should be. unix2dos is a venerable and pretty much bulletproof utility to handle this, so your solution sounds solid. If you can check the return code from the shell call, it will make your program more robust...

Steve

[small]"Every program can be reduced by one instruction, and every program has at least one bug. Therefore, any program can be reduced to one instruction which doesn't work." (Object::perlDesignPatterns)[/small]
 
Alternatively you could use the FileSystemObject, which recognozes and correctly handles both Windows and Unix line terminators
 
The use of the unix2dos command not only allows me to read the file, it also corrects the file so that it can then be sent to and viewed by clients in their various software. Thanks to everyone for your help.

Paul H.
 
Another elegant technical solution dashed on the rocks of incomplete requirements specification...[smile]

Steve

[small]"Every program can be reduced by one instruction, and every program has at least one bug. Therefore, any program can be reduced to one instruction which doesn't work." (Object::perlDesignPatterns)[/small]
 
I was operating under the mistaken impression that unix2dos is part of a standard Windows installation, which it is not. So I have tried the FileSystemObject route and that does read the file in correctly. Can it also correct the file (from unix terminators to dos terminators) on the fly as unix2dos does or do I have to write out each line to a new file and then rename it?
 
unix2dos reads the file, writes out a temporary, deletes the original, and renames the temporary back to the original name. So it doesn't really 'do it on the fly' as you suggest, and you will have to do what it does I'm afraid...

Steve

[small]"Every program can be reduced by one instruction, and every program has at least one bug. Therefore, any program can be reduced to one instruction which doesn't work." (Object::perlDesignPatterns)[/small]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top