Suppose you are working on a very old unix server where dos2unix
, perl
, tr
, and sed
are not present. How can you convert files from dos to unix format?
Asked
Active
Viewed 3,483 times
9

user664833
- 209

Hemant
- 6,910
- 5
- 39
- 42
2 Answers
12
I think you are referring to removing the caret-M at the end of lines. You can use search and replace in vi to do this.
In vi I normally do: (where "^" represents CTRL):
:%s/^V^M//g
Which shows on the screen as:
:%s/^M//g

jjclarkson
- 2,147
-
-
4You can also use sed to do the same thing w/out having to vim the file: sed -e '%s/^V^M//g' filename
That also will show on the screen as sed e '%s/^M//g' filename In general, if you can search/replace it in vim, the command is virtually the same in sed. – gabe. Aug 12 '10 at 19:09 -
2
-
@wzzrd,
sed
andvi
are both specified by POSIX, and thatvi
command doesn't use any Vim extensions. – Wildcard Apr 16 '16 at 02:11 -
-
@gabe. is my sed to old to do this
$ sed -e '%s/^M//g' cookies sed: -e expression #1, char 1: unknown command:
%' ` ? – cokedude Apr 28 '16 at 22:11
6
A server without tr
or sed
would have to be really old, or missing some basic commands. Hopefully ed
is there; it existed in Unix first edition.
ed /path/to/file
1,$s/^V^M$//
w
q
where ^V^M
means typing Ctrl+V then Ctrl+M (to enter a literal line feed). If you know that all lines do end in CR LF, you can use 1,$s/.$//
instead (indiscriminately remove the last character on each line).

Gilles 'SO- stop being evil'
- 829,060
tr
andsed
?sed
is old........ iirc, – xenoterracide Nov 07 '10 at 23:25ex
ored
be available on a system like that? I would'nt call it unix if there is notr
orsed
. – MattBianco Mar 31 '11 at 06:52sed
? Really!? Out of interest, what is the system? As Gilles and MattBianco point out, it would probably still haveed
at least. – Mikel Mar 31 '11 at 21:03