Remove ^M character from log files.
In my script I redirect output of my program to a log file. The output of my log file contains some ^M (newline) characters. I need to remove them while running itself.
My command:
$ java -jar test.jar >> test.log
test.log
has:
Starting script … ^M Starting script …Initializing
Best Answer
Converting a standalone file
If you run the following command:
The
<file>
will have all the ^M characters stripped. If you want to leave<file>
intact, then simply rundos2unix
like this:Parsing output from a command
If you need to do them as part of a chain of commands via a pipe, you can use any number of tools such as
tr
,sed
,awk
, orperl
to do this.tr
sed
awk
perl
Typing ^M
When entering the
^M
be sure to enter it in one of the following ways:\r
).\015
).\x0D
).Why is this necessary?
The
^M
is part of how end of lines are terminated on the Windows platform. Each end of line is terminated with a carriage return character followed by a linefeed character.On Unix systems the end of line is terminated by just a linefeed character.
0x0A
in hex, also written as\n
.0x0D
in hex, also written as\r
.Examples
You can see these if you pipe the output to a tool such as
od
orhexdump
. Here's a sample file with the line terminating carriage returns + linefeed characters.You can see them with
hexdump
as\r
+\n
:Or as their hexidecimal
0d
+0a
:Running this through
sed 's/\r//g'
:You can see that
sed
has removed the0d
character.Viewing files with ^M without converting?
Yes you can use
vim
to do this. You can either set thefileformat
setting in vim, which will have the effect of converting the file like we were doing above, or you can change the fileformat in thevim
view.changing a file's format
You can use the shorthand notation too:
Alternatively you can just change the fileformat of the view. This approach is nondestructive:
Here you can see me opening our
^M
file,sample.txt
invim
:Now I'm converting the fileformat in the view:
Here's what it looks like when converted to the
unix
fileformat:References