Link to home
Start Free TrialLog in
Avatar of thepadders
thepadders

asked on

Mass change line endings?

Is there a way to mass change line endings from Windows to Unix? I develop on Windows but run on Unix and this is sometimes a problem.

I don't want to do a str_replace based on \n \r because sometimes line endings are hardcoded into the script (e.g. for emails etc).

Any suggestions?
Avatar of Muhammad Wasif
Muhammad Wasif
Flag of Pakistan image

What problems you are facing running your code on Unix?
Avatar of thepadders
thepadders

ASKER

The main problem I had is for scripts that where going to be run from the command line. Even with the correct #!/usr/bin/php the file would suggest that this was wrong. Changing the file to unix line endings and it worked.

So far I have only changed the files manually that are run by the command line, but it seems to be a bit inconsistant to ship a package with some files with unix line endings and others with windows.
The easiest way to do this is via FTP in text mode
The ftp server does it all for you.
If you distribute your code this way then the results will be OS compatable also
Hi,

FTP in text mode? What upload to a linux server and then back again? The files are stored in subversion respository and exported from that for distributon, I am not sure that FTPing up and then down and trying to overwrite the repos is going to work.
No that would do it both ways and you get back what you started with.
I guess you could upload in text mode then download in binary :) That seems a little silly, but it would work.
 
But what I ment was, when you want to move the files from your windows box to the server or distribute your files to other computers.
use FTP for the transfere the relvant scripts to the server, then they are allways in the correct \n format.

There was a script I had years ago called prettyperl which did what you want for perl maybe you could google it sorry no time to do it now
There also is another one called dos2unix

These probably need to be run on the unix command line (sorry I haven't used them in years) and you can feed them stuff like *.php as an argument
You could adjust them to give you the right carriage returns
Also you could phpize them if you don't have perl on your windows box. They are pretty straight forward scripts from memory


 
ASKER CERTIFIED SOLUTION
Avatar of spotx
spotx

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
In vi, you can load the file and issue the following

:set fileformat=unix
:w

Should be able to script that, too

Avatar of Roonaan
You can also probably set your edit software on your windows to save files as unix. Saves you all the trouble.

-r-
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Those kind of tools are also build in, when you have a proper texteditor.

-r-
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Lots of text editors could do the conversion but trying to do it to a hierarchy of files, en-mass would be difficult using a text editor.  I'd think a command line tool like unix2dos would be better.  It can operate on wildcard filename patterns.
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
define('EOL', 0x0d . 0x0a);

is probably better as ...

define('EOL', "\r" . "\n"); // separated to make sure you NEVER find \r\n together in 1 place.

As the questioner needs to preserve REAL end of lines for email but allow normal eols to be converted, using a unix2dos / dos2unix mechanism combined with a define for the REAL eols is probably the best solution.

With that regard, a split between RQuadling and spotx.
RQuadling has good advice regarding , "NOT use \r\n in code".  However, given that the author has already hard coded those characters into his source, it would seem that this is an unsolvable problem.  The question degenerates into, "how can I remove the \r characters I don't want without removing the \r characters I do want?"

Programmatically, there's no way to determine the author's intent for each \r character.  Somebody will have to manually change the desired \r characters as RQuadling has suggested.  Once that is done, any of the other suggested methods for removing the unwanted \r characters would work (i.e. FTP, search/repleace, dos2unix, etc.)  Again, RQuadling pointed that out earlier.

I guess I like RQuadling's advice to this no-win situation.  The other responder's might deserve an "assist".  Personally, I'm fine with 0 points for me.
It would seem to me that the whole disscussion is a good one with valiable information to show the issues associated with new lines and how to deal with them in different environments.
It would be good if the discussion could be given a clearer name so that when someone had these issues a search for somethinig like 'newline issues different OS' was searched for this disscussion would come up.
I know that years ago when I needed this info I also would have had trouble working out what to search for in a knowledge base like this.
It would be a pitty to have it lost never to be retrieved again.