FreeBSD Linux Tips and Tricks

How to get rid of ^M characters.

When you edit a file in Windows and then open in UNIX, you must have seen ^M characters getting appended in the content. How to get rid of it ?

Most UNIX OS come with a utility called ‘dos2unix or dosunix (FreeBSD)’ . If it is not installed, please run following appropriate commands to install the utility and then convert appropriate file

  • Linux:

             yum install dos2unix

             dos2unix inputfile  >outputfile

  • FreeBSD:

            pkg install dosunix

            dosunix inputfile outputfile

Also, there are other ways to remove such characters as well:

  • Via Sed command:

           sed -e “s/^V^M//” inputfile > outputfile

          Note: Don’t paste the above command. Press Ctrl V and then Ctrl M in the above command instead.

  • Via vi editor:

    1. Open the file in vi.
    2. In the command mode, type :%s/^M//g.

          Note: Don’t type “^M”, but instead press “Ctrl V” and “Ctrl M” for it.

Related Articles

  • Vi Commands 101

    Cursor movement h – move left j – move down k – move up l – move right w – jump by start of words (punctuation considered words) W –...
  • Add IP to CentOS 5/6

    If you recently got a secondary IP block for your server but you do not know how to bind the secondary IP address, please follow the steps below:   First, run...
  • Simple Stateful Load Balancer with iptables and NAT

    NOTE: To demonstrate how iptables can perform network address translation this how-to shows how to use it to implement a over-simplified load balancer. In practice we would use a daemon...
  • Using Top [ $top ]

    What is Top? Top provides a live display of information on the CPU processes. Top gets updated periodically as it provides an output CPU level percentage, Memory size, Swap Info,...