2012-03-06

Removing ^M Characters from Files in ESXi

As part of a build process for an ESXi server – on of the stages are to upload a valid SSL certificate to the ESXi server.
When copying the certificate over to the host with pcsp for some reason they file is always malformed when going over. If you do a cat rui.crt you will see no issue, but if you do a vi rui.crt then you will see that each and every line has a ^M at the end of it – this is because the file is a dos format file.
dos format
I finally found a to remove them – and it was not easy to find.
Both tr and dos2unix are not available on ESXi
sed 's/'"$(printf '\015')"'$//
s/'"$(printf '\032')"'$//' rui.crt > rui.new

That produces a clean file.
unix format
Phew – now I can rest easy…

Update:
Thanks to the comment received from JR below it is even easier

sed 's/.$//' < rui.crt > rui.new

5 comments:

JR said...

I've not got ESXi with that file in front of me right now, but you might find it quicker to load the file in vi, hit Escape, then type:

:s/.$//

This performs a substitution (the "s") where it looks for any single character (the ".") followed by the end of the line (the "$") and replaces it with nothing.

Maish said...

Thanks for your comment JR. I just tried that on an ESXi box - no go..

Also I am not aware if it is possible to script that interaction in vi

Ed Grigson said...

I think you need :%s, not just :s. I'm no VI guru but most things are scriptable. You may also need a trailing 'g' to make the replace global.

Another alternative is to use Notepad++ to convert the file to Linux format prior to copying the cert across.

JR said...

That's odd - must be due to the vi version on ESXi as it works on "proper" Unix :-)

Having said that, the regexp does appear to work in sed on ESXi:

sed 's/.$//' < rui.crt > rui.new

Maish said...

Thank you very much - that works perfectly.

Will add it to the post above!!