Linux text dos format to UNIX format, remove ^m symbols

Source: Internet
Author: User

Q: I pass a text file to Linux in Windows via FTP, but after opening the text file, each line has a ^m flag at the end. Because of the long, with the editor to remove too much trouble, what is the solution?

A : In order to solve this problem, Linux has a special two tools to swap Windows format and Linux format, which are Dos2unix and Unix2dos respectively. For example, use the following command to convert a file named "FileName" from the Windows format to the Linux text format.

Text:

under Linux, it is unavoidable to use vim to open some edited text files under Windows. We will find a ^m symbol at the end of each line of the file, because the DOS editor and the Linux editor are inconsistent with the carriage return at the end of the file line .

for a carriage return definition:
windows:0d0a
unix/linux:0a
mac:0d

There are several ways to remove these symbols more quickly:

(1) is the command with VI:
open a text file with VI
VI dos.txt
Command mode input
: Set Fileformat=unix
: w

(2) VI using regular expressions to replace
g/^m/s/^m//
or
%s/^m//g

to delete all the ^m symbols, you can use the following command for VI:
:%s/^v^m//g
wherein, ^V refers to Ctrl + V, ^m refers to Ctrl+m
The final effect is:
:%s/^m//g
or use the following command:
:%s/\r//g
where \ r represents carriage return character

(3) using the SED tool
sed ' s/^m//' filename > Tmp_filename

(4) Since the window under the carriage return more ", then of course, by deleting", can also be implemented:
tr-d ""

(5) The last method is my most commonly used method, personally feel most convenient
under the terminal Knock Command:
$ dos2unix FileName
switch directly to UNIX format and it's OK! ~

on the web, we searched for an article that will transform the line between UNIX and Windows, learning to add:
Grammar
Dos2unix [-kn] file [newfile] transition from DOS to UNIX
Unix2dos [-kn] file [newfile] transitioning from UNIX to DOS or Windows
-K: Retains the original mtime time of the document (does not update the last modified time of the document)
- N: Keep the original document and enter the converted content into the new document for example: dos2unix-n old New

examples are as follows:


$cat-v killws#!/bin/bash^madmin_dir= "/home/admin" ^mfunction check_user {^m if [[' whoami '! = ' admin ']];then^m Echo ' Apac He can start by admin user,exit! " ^m exit^m fi^m}^m$dos2unix-k killwsdos2unix:converting file killws to UNIX format ... $cat-v killws#!/bin/bashadmin_dir=  "/home/admin" function check_user {if [[' whoami '! = ' admin ']];then echo ' Apache only can start by admin user,exit! ' exit Fi



Linux text dos format to UNIX format, remove ^m symbols

Related Article

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.