6

I use an old server without dos2unix and I would like to convert files containing Windows-style end-of-line (EOL) to Unix-style EOL. 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?

1
  • 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. Commented Apr 28, 2016 at 22:30

3 Answers 3

14

If you have GNU sed you can do this:

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

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

8
  • 3
    \r is supported by sed, too. Why use harder-to-remember number when you can use simpler notation? Commented Apr 28, 2016 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
    Commented Apr 28, 2016 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
    Commented Apr 28, 2016 at 22:30
  • 1
    What are you trying to do with "%" ? Your are not in vim ;-).
    – gapz
    Commented Apr 28, 2016 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
    Commented Apr 28, 2016 at 23:05
5

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.

4

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

sed -i 's/.$//' script
1
  • 1
    Note that if you run this on a Linux-style EOL file, this answer will effectively destroy it (the sed command simply strips off all last characters). This behaviour is different from the dos2unix tool which ignores files that don't contain Windows-style line endings.
    – Edward
    Commented Nov 4, 2022 at 11:46

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .