1

I use an old server without dos2unix and I would like to convert windows files to unix files. I am unfortunately not the admin so I can't install dos2unix. The tr method seems to be the only one that works.

cp script _p4 && tr -d '\r' < _p4 > script && rm _p4

Are there any easier methods to do this? I have a really hard time either remembering this or searching for this.

  • dos2unix is a small and relatively self-contained binary. There's a sporting chance it will work on your server. If you have dos2unix installed on a machine with the same arch and OS (is this Linux?) then try copying it to your home directory and do ./dos2unix file_you_want_to_convert. Or, of course you could just download a suitable binary to the server directly. – Faheem Mitha Apr 28 '16 at 22:30
3

If you have GNU sed you can do this:

sed -i 's/\x0D$//' script

Where "x0D" is the ASCII code for \r.

  • \r is supported by sed, too. Why use harder-to-remember number when you can use simpler notation? – Alexander Batischev Apr 28 '16 at 22:17
  • I used to think that \r was not supported by all sed version, especially the GNU one. Things have changed, but I still use something which can be more portable to all sed version (maybe I'm wrong). – gapz Apr 28 '16 at 22:22
  • @gapz any idea why this one won't work sed -e '%s/^M//g' cookies sed: -e expression #1, char 1: unknown command: %' ` ? Is my sed to old? – cokedude Apr 28 '16 at 22:30
  • What are you trying to do with "%" ? Your are not in vim ;-). – gapz Apr 28 '16 at 22:36
  • Saw that on another page unix.stackexchange.com/questions/411/…. Couldn't get it to work so I asked my question here. – cokedude Apr 28 '16 at 23:05
2

You can always write a script:

#!/bin/sh
for name in "$@"
do
    cp "$name" "$name"~ && tr -d '\r' < "$name"~ > "$name" && rm "$name"~
done

and name that dos2unix. No compiler is needed.

2

This command can be used to convert EOL characters without having dos2unix installed:

sed -i 's/.$//' script

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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