How to remove Ctrl-M from files where dos2unix, perl, tr, and sed are not present?

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?

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

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

Method 2

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).


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x