[Gate-users] Cause of problems in reading text files (e.g. GATE macros) at Linux OS after they have been read or written at Windows platform - Solution provided using command "tr"
Nicolas Karakatsanis
knicolas at mail.ntua.gr
Tue May 27 23:58:25 CEST 2008
Dear GATE users,
I had encountered in the past minor but annoying problems when I was
trying to use GATE macro files on a Linux distribution that has
been previously opened or written using Windows OS.
The problem is caused by a hidden end of line character (EOL) at the
text files, which is added from Windows in order to indicate change of line.
Most of the times Windows add the hidden characters CR (carriage return)
and LF (line feed) to seperate lines between them.
That means that whenever an editor on Windows sees the sequence CR+LF
will place the next character on the next line.
However the equivalent line seperator for Linux is LF only. CR is not a
special character for Linux!
Therefore when GATE (or any other Linux application) tries to read the
text files, it interprets CR as a part of the command line and NOT as a
special character. A Linux text editor will read the special
character LF , after the character CR, and change line in the text file.
Therefore all the command lines ends with a non-special character CR
(you can see it as ^M or \r in some advanced Linux text editors)
If we delete those characters and leave LF intact then Linux will be
able to read them properly.
Some macro files however are quite long and therefore it is impractical
to repeat that tedious procedure for every line of every macro file we
import from Windows.
The solution is simple and comes directly from what is mentioned above...
There is a simple Unix command which is supported by all Linux
distributions, which performs basic operations to the ASCII characters
of a text file... this command is tr...
If your text file has been read or written in Windows then the line
separator will be the sequence of CR+LF characters. Therefore you simply
need to delete the CR character (or \r) and leave the LF character (\n)
intact. To do that just type:
tr -d `\r` <input_filename> output_filename
For example if someone sent you a Macrofile with the name :
Lu_spectrum.mac and you have error messages in GATE like:
command ignored, command not found
then simply go to the directory where the Macrofile is located and try
typing:
tr -d `\r` <Lu_spectrum.mac> Lu_spectrum.mac
If for some reason the line separator of your text files is only CR then
you need to replace CR with LF to read it correctly at Linux. In that
case type
tr `\r` `\n` <input_filename> output_filename
Some additional advices based on the following problem:
1) Avoid using Notepad when reading Linux files... you might get a text
file with just one line (no line seperators)
Fortunately the windows application of Wordpad does not have such problem
2) Always type <Enter> right after the end of the last line of your
Linux text file. This way you make sure the file ends up with a line
seperator and no problems will occur with the execution of the last
command. Some applications find it difficult to distinguish between
EndOfFile (EOF) and EndOfLine(EOL) characters. By placing an EOL at the
very end of your text file you make sure that there will be no problems.
Best regards
Nicolas
___________________________________________________
Nicolas Karakatsanis, PhD Candidate
Biomedical Simulations and Imaging Technologies Laboratory
School of Electrical and Computer Engineering
National Technical University of Athens, Greece
More information about the Gate-users
mailing list