Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I have a bunch of PHP scripts that are in DOS format (vi displayed [noeol] [dos] in the status line) and as expected they weren't usable.

I ran dos2unix over them to fix the line endins:

dos2unix index.php

but when I open the files in vi I still see [noeol] on the status line.

Upon checking them in vi's hex editor mode (:%!xxd) I don't see anything that jumps out at me.

In the original files the line ends looked like: 0x0D 0x0A

If after running through dos2unix they look like: 0x0A

I also checked to ensure that the last line of the file has a proper end of line and that's there too (0x0A).

What else could be causing this, should I worry?

I'm running CentOS 5.6 x64.

share|improve this question
add comment

5 Answers

up vote 4 down vote accepted

As @Klox said, after running dos2unix, the remaining problem vi shows is the missing newline on the last line.

If you open the file in vi and save it, it will add the newline for you.

Or in a batch you can do the whole thing with ed:

#!/bin/bash
for f in <list-of-files-goes-here>; do
    ed -s -- "$f" <<<$',s/\r//g\nw' >/dev/null
done
share|improve this answer
    
Thanks, I knocked up a variation in that. Is dos2unix broken? –  Kev Aug 25 '11 at 15:29
    
@Kev: i don't know, i consider it an un-useful tool, given many other methods exist to do the same, without installing nothing new. –  enzotib Aug 25 '11 at 15:50
1  
I eventually did: vi +":w ++ff=unix" +":q" ${f} in a loop. –  Kev Aug 25 '11 at 15:52
add comment

It is just warning you that you don't have an end of line (\n) on the last line.

share|improve this answer
    
That was the first thing I checked and it's there. –  Kev Aug 25 '11 at 13:56
    
@Kev: when you save, vi insert the last line newline. Have you checked after saving? –  enzotib Aug 25 '11 at 14:04
    
Yes that works as does :set ff=unix, but I have over 200 files and wanted to batch convert them. –  Kev Aug 25 '11 at 14:10
    
Use a shell for loop to run echo >> myfile on each file. –  Klox Aug 25 '11 at 15:14
add comment

DOS machines enter a carriage return at the end of lines (eol), it should look like this in vi ^M.

Try running this:

:%s/^M//g
share|improve this answer
add comment

You may also use ed to batch convert your php files since ed will add the missing final newlines for you as well.

Using ed there is no need for tmp files either.

# using Bash
IFS=$'\n'
for file in *.php; do
   ed -s "$file" <<< $'H\n,g/\r*$/s///\nwq'
   #printf '%s\n' H $',g/\r*$/s///' wq | ed -s "$file"
done
share|improve this answer
    
I would point out that I came to almost the same solution without seeing your answer :) –  enzotib Aug 25 '11 at 15:51
add comment

If you use vim (instead of an old skool vi), you can do this:

:set ff=dos
:e!

and you'll be able to edit the PHP files without all the ugly "^M" suffixes. I believe it will save as a DOS "text file" format, too. This can be useful if you share the files with (ugh!) Windows somehow.

A little editorializing: the two-byte end-of-line token chosed for MS-DOS has got to have been one of the biggest problems that MS-DOS foisted off on the world, after segments, drive-letters and "\" as directory seperator. That two-byte token is still the reason for a difference between "text" and "binary". Ickk.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.