• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 472
  • Last Modified:

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?
0
thepadders
Asked:
thepadders
  • 4
  • 3
  • 3
  • +5
4 Solutions
 
Muhammad WasifCommented:
What problems you are facing running your code on Unix?
0
 
thepaddersAuthor Commented:
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.
0
 
spotxCommented:
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
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
thepaddersAuthor Commented:
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.
0
 
spotxCommented:
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


 
0
 
spotxCommented:
Sorry my solution didn't consider the repository as the distro source, but the FTP idea will work your editor may spit it if you only have newlines with no carriage returns.

you could write a script that will go through each file
read them in line by line
rtrim each line with "\n\r" as the second argument thus the only carriage returns that would be removed would be the last ones on the line.
and save them to a different file adding the correct line endings

Also I could be wrong but I think php in windows will handle the unix carriage returns but that I am not sure of
If that is so you could run your dos2unix php script on all the files and they would work on either OS.

Hope some of that helps
0
 
tdterryCommented:
In vi, you can load the file and issue the following

:set fileformat=unix
:w

Should be able to script that, too

0
 
RoonaanCommented:
You can also probably set your edit software on your windows to save files as unix. Saves you all the trouble.

-r-
0
 
WilliamFrantzCommented:
There are standard unix commands called 'dos2unix' and 'unix2dos' that fix line endings.  They are available for Windows as part of the cygwin package.

http://cygwin.com
0
 
RoonaanCommented:
Those kind of tools are also build in, when you have a proper texteditor.

-r-
0
 
DenisvtCommented:
Proper editor as in "EditPlus", I confirm it takes care of such issues.
0
 
WilliamFrantzCommented:
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.
0
 
Richard QuadlingSenior Software DeverloperCommented:
THe issue though (as you identified in your question) is that you want to preserve REAL \r\n.

The solution is to NOT use \r\n in code ("\r\n").

Instead create a constant called EOL

define('EOL', 0x0d . 0x0a);

$s_lines_with_EOL = 'Line 1' . EOL . 'Line 2' . EOL;

Unfortunately, you will not be able to use heredoc anymore ...

$s_lines_via_heredoc_but_need_EOL = <<< END_STRING
Line 1
Line 2

END_STRING;

Once you've got the EOL's written correctly, you can then use many applications to convert.

I use EditPadPro (http://www.editpadpro.com).

You can load up the entire project and do a global search and replace in 1 go.

Then tell it that PHP files are unix style files and you're done!
0
 
Richard QuadlingSenior Software DeverloperCommented:
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.

0
 
Richard QuadlingSenior Software DeverloperCommented:
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.
0
 
WilliamFrantzCommented:
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.
0
 
spotxCommented:
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.
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 4
  • 3
  • 3
  • +5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now