Remove the ^ M symbol from the Linux text file

Source: Internet
Author: User

Remove the ^ M symbol from the Linux text file

In Linux, it is inevitable to use VIM to open some edited text files in Windows. We will find that there will be a ^ M symbol at the end of each line of the file, because the editor in DOS and the Linux editor are inconsistent with the carriage return character at the end of the file line.

Definition of carriage return:

Windows: 0D0A

Unix \ Linux: 0A

MAC: 0D

There are several methods to quickly remove these symbols:

(1) Use the VI command:

Use vi to open a text file

Vi dos.txt

Input in command mode

: Set fileformat = unix

: W

(2) Replace with a regular expression in VI

G/\ ^ M/s/\ ^ M //

Or

% S/^ M // g

(3) Use sed Tool

Sed's/^ M // 'filename> tmp_filename

(4) Since there are more carriage returns '\ R' in the window, you can also delete' \ R:

Tr-d '\ R'

(5) The last method is my most commonly used method. I personally think it is the most convenient.

Run the following command on the terminal:

$ Dos2unix filename

Convert it to the unix format !~

This article permanently updates the link address:

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.