How to remove CTRL-M (^M) characters from a file in Linux
This document (7014821) is provided subject to the disclaimer at the end of this document.
Environment
SUSE Linux Enterprise Server 11
Situation
How to convert plain text files in DOS/MAC format to UNIX format.
Newly created SSL Certificates not working properly.
Certs invalid or not properly configured, agents unable to use.
Application reporting obscure syntax errors and other unanticipated, unforeseen errors.
Viewing the certificate files in Linux shows ^M characters appended to every line.
The file in question was created in Windows and then copied over to Linux.
^M is the keyboard equivalent to \r or CTRL-v + CTRL-m in vim.
Resolution
- The simplest solution, use the dos2unix command (sometimes named fromdos, d2u or unix2dos):
dos2unix filename
- Using the stream editor sed:
sed -e "s/\r//g" file > newfile
- Using perl:
perl -p -e 's/\r//g' file > newfile
- Using a terminal editor vi or vim:
Inside vi [in ESC mode] type: :%s/^M//g
Note: To enter ^M, type CTRL-V + M. That is, hold down the CTRL key then press V and M in succession.
Cause
Additional Information
cat -v <filename>
-----BEGIN CERTIFICATE-----^M
MIIDBjCCAe4CCQD3cWtEoYMtujANBgkqhkiG9w0BAQUFADBFMQswCQYDVQQGEwJB^M
VTETMBEGA1UECBMKU29tZS1TdGF0ZTEhMB8GA1UEChMYSW50ZXJuZXQgV2lkZ2l0^M
cyBQdHkgTHRkMB4XDTEzMDcxNzIxMTU0NFoXDTE1MDcxNzIxMTU0NFowRTELMAkG^M
A1UEBhMCQVUxEzARBgNVBAgTClNvbWUtU3RhdGUxITAfBgNVBAoTGEludGVybmV0^M
IFdpZGdpdHMgUHR5IEx0ZDCCASIwDQYJKoZIhvcNAQEBBQADggEPADCCAQoCggEB^M
ANIQ4wo6zi9lY5WddpJbHO2OvXgbJZIB8qidksKrFm3tUMe6LiXpOb4fZAwqlUUp^M
fN+jrp+Lr7PPF52vnbKMP4dq31CWV68EtcRK3O8maLy4mpx7pCXeZIqRw3/1UwIr^M
RXzOCWTsdvudAC0Sp0DgN8U7qECojZso9nu1SBopOtBDDU/Wcdmd3ydZJN0R7waB^M
e38WNK6rUNCgcIP0obvuiwppJmUyE1hr09A3W2zGB6bMF4EqZ5uaiBoPhMyKTlDs^M
UMOd6tRXkrYEdnLuqQttUvtE6sfxZaUTmod5MCXSRrW0tAkBGhqlPBT8NvlRQzaz^M
TqzLqhCYPQ9HBihumKHRkucCAwEAATANBgkqhkiG9w0BAQUFAAOCAQEALAGKt8Bn^M
PHYPj6RNioE8eyCvx4WFG8zDIkbkFbmpBiq6SOpWliCnhGWyH5yFEDWHCOebz8L9^M
LHQTi18KDGk7PH0BpWZDpksu3JrBb3fjm+1QCpD9KWRYJVnBGAaWFFmPPpJJDoz2^M
X0MRPbzxfqsno68l46X+5l5m4H8Y2g0cOUto580x/ZlNb+gHbTzuBnwGa2Oof0bc^M
A7ZmU1IytADXWVM28h88XcBooT4dwxCVkwznNGQ8lQ+XTehjdcDEEvbTB/Jc+1M4^M
0a/OvmlDIFU34DKH/LRc7bmTe4YTTRtU5ShXTBH0ChZFuqP20vtiEsPHVn+pk4LN^M
129c5uhrxPGhNQ==^M
-----END CERTIFICATE-----^M
For an in-depth explanation of line endings, see Newline from Wikipedia.
Disclaimer
This Support Knowledgebase provides a valuable tool for SUSE customers and parties interested in our products and solutions to acquire information, ideas and learn from one another. Materials are provided for informational, personal or non-commercial use within your organization and are presented "AS IS" WITHOUT WARRANTY OF ANY KIND.
- Document ID:7014821
- Creation Date: 31-Mar-2014
- Modified Date:23-Feb-2021
-
- SUSE Linux Enterprise Server
For questions or concerns with the SUSE Knowledgebase please contact: tidfeedback[at]suse.com