Remove ^M (CTRL+M) from a file in Linux


Unix and windows file formats are different. Sometime a Unix file which edit in Windows put a special character at the end of each line i.e ^M (CTRL+M). It can make that file useless sometime.

But no worry you can fix that with ease, just follow the procedure given below:

  • Open the file in vim or vi
vi myfile
  • Then press ESC key, and type
:%s/^M//g
  • Remember ^M is a special character which can be generated as given in NOTE.

sed command can also be used for this as follows:

sed 's/^M//g' myfile> newfile

^M is also a special character here.

That’s all, your file is same as last.

NOTE: Hold the control key and then press v and m to get the ^M (CTRL+M) character.

Advertisements

2 thoughts on “Remove ^M (CTRL+M) from a file in Linux

  1. …or just run the command ‘dos2unix myfile’. (The ^M is actually a carriage return, while only line feed is needed on unix-like systems.)

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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 )

Google+ photo

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

Connecting to %s