Replacing ^M with line break (carriage return or newline) to make viewing log files easier


Some syslog log files produced  contain  ^M  instead of a newline (or carriage return) at where a line ending would normally be.

Systems which use this pattern include the good old Cisco VCS – still my favourite SIP/H.323 call control platform – and also log files from exciting new video conferencing disruptor Pexip – easily my favourite MCU and imho far and away the “best of breed” of next generation video bridges- although I don’t pretend to be in any way unbiased here!

Whilst the use of ^M is actually very useful (as it allows you to easily use powerful command line tools like “grep” to find & filter relevant/interesting logs when troubleshooting) it can mean that reading the logs  you’ve found/filtered that way can be a little bit difficult – particularly if the log contained several  lines  e.g. as a any SIP message would.

There are a number of ways of cleaning these logs  up to make the logs easier for poor humans to read:

Command line:

From the command line this is easily achieved with the “sed” tool found on most Unix systems:

    cat /var/log/my_logfile.log  | sed “s/\^M/\n/g” > /tmp/my_easy_to_read_logfile.txt


In Emacs (manually):

If you’re using the big granddaddy of text editors to view the log file, you can tidy up what you’re seeing using the following incantation:

    M-x replace-string RET ^M RET C-q C-j

For some reason, I struggle to remember the C-q C-j part  – so, if like me, you have a goldfish-like memory you may prefer to wrap this up in a macro:


In Emacs using a ‘helper’ emacs macro:

Add this to  your .emacs file, save it,  and restart emacs:

    (defun replace-ctrlms ()
    (replace-string “^M” “\n”)


Now open the log file you want to clean up, and do:

  M-x replace-ctrlms  RET

Job done – and you now have a slightly easier to remember “command” for doing this in future.


In vim/vi:

If you’re using the great grandma of text editors to view the log file, the following should do more or less what you need:

  ESC :%s/\^M/\r/g RET




Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s