[Linux]: How do I remove ‘Control M’ i.e., (^M) characters from a file

To create a file with “Control M” (^M) characters – you can type Ctrl+V,Ctrl+M from keyboard through vi editor in a linux file.
And to view “Control M” characters in a file, you can use “cat -v filename” or “cat -A filename” ; you can not usually see them with normal cat command.

[Dev root @ dbversitydotcom /tmp]# cat -v dbversity.txt

^M
^M ^M

==============
Amazon AuroraDB
PostgreSQL
mariaDB ^M
==============

============================
Oracle & MS-SQL
MongoDB^M
============================

^M^M^M

[Dev root @ dbversitydotcom /tmp]

tr command to remove Control M characters.

[Dev root @ dbversitydotcom /tmp]# tr -d ‘\r’ < dbversity.txt > new_dbversity.txt
[Dev root @ dbversitydotcom /tmp]#
[Dev root @ dbversitydotcom /tmp]# mv new_dbversity.txt dbversity.txt
mv: overwrite `dbversity.txt’? y
[Dev root @ dbversitydotcom /tmp]#
[Dev root @ dbversitydotcom /tmp]# rm new_dbversity.txt
[Dev root @ dbversitydotcom /tmp]# cat -v dbversity.txt
==============
Amazon AuroraDB
PostgreSQL
mariaDB
==============

============================
Oracle & MS-SQL
MongoDB
============================
[Dev root @ dbversitydotcom /tmp]

Another solution : dos2unix command will fix it.

dos2unix filename

Reason for ^M characters in a file :

Windows-based text editors put special characters at the end of lines to denote a line return or newline. Normally harmless, some applications on a Linux server cannot understand these characters and can cause the service to not respond correctly. This can result in unforeseen complications. This is because the file is created or perhaps even edited on a machine running Windows and then uploaded to a Linux server.

Example : (rsa) public/private keys

—–BEGIN CERTIFICATE—–^M
ZXIIDBjCAe4CCQD3cWtEoYMtujANBgkqhkiG9w0BAQUFADBFMQswCQYDVQQGEwJB^M
VTETMBEGA1UECBMKU29tZS1TdGF0ZTEhMB8GA1UEChMYSW50ZXJuZXQgV2lkZ2l0^M
cyBQdHkgTHRkMB4XDTEzMDcxNzIxMTU0NFoXDTE1MDcxNzIxMTU0NFowRTELMAkG^M
A1UEBhMCQVUxEzARBgNVBAgTClNvbWUtU3RhdGUxITAfBgNVBAoTGEludGVybmV0^M
IFdpZG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