4 Ways to Remove M Characters “Control M” or “^M” or “CTRL M” Characters in a UNIX file

September 8, 2012 5:50 pm4 commentsViews: 6838

ohh do you want to remove M characters in UNIX ? Though i am not so good in UNIX but this time i had to get my hand dirty in UNIX.

Reasons were, there is no one to support things from UNIX perspective. And all of sudden I got 20+ UNIX scripts to work upon. You can imagine that what would have happened to me. I am in one among them who does not even like to see that raw black screen. It is a bizarre for me!!!!!  This happens when you import files from MS-DOS to UNIX system and is a very common issue,                      

Objective of this post is to address below questions

  1. Why I am seeing ^M (control M ) character in UNIX file ?  
  2. How to make my UNIX script  \ file clean and without ^M (control M) character?
  3. What’s the best method to remove M characters or ( ^M ) from any UNIX file ?
You can do it by two ways. Script or manual, and my vote will be for Script as it has only one step as opposite to manual method where we need to perform two steps. Moreover it helps you to be calm when everyone in a team is spoiling files on which you are supposed to work !!!!!!!.  

Remove M Characters (^M) with Unix commands 

Using dos2unix  command

dos2unix ORIG_FILENAME TEMP_FILENAME 
mv TEMP_FILENAME ORIG_FILENAME 
IMP NOTE: use dos2ux command if you are using HPUX 

Using sed command

sed ‘s/^M//g’ ORIG_FILENAME > TEMP_FILENAME 
mv TEMP_FILENAME ORIG_FILENAME 
IMP NOTE: To get ^M in UNIX (Hold control key and then press v and m character). 
Using vi Editor 
ESCAPE  :%s/^M//gENTER 
IMP NOTE: To get ^M in UNIX (Hold control key and then press v and m character). 

Remove M Characters (^M) with Unix Script 

You can put above command in UNIX and it will do wonder as now you just have to give one parameter to that script. A script code is below and looks at the picture to understand that how it works.

#!/bin/sh
TEMPORARY _FILE=”/TEMP_FOLDER/$$”
if [ $# -ne 1 ] ; then
echo “USAGE $0 Provide-Dos-File-Name”
exit 4
fi
dos2unix $1 > $TEMPORARY_FILE && mv $TEMPORARY_FILE $1
#############################################################
### Enable below for HPUX
#dos2ux $1 > $TEMPORARY_FILE && mv $TEMPORARY_FILE $1
#############################################################
UNIX script to remove control m characters

Unix Script for Removing CTRL M Character – Screen Shot

To Remove m Characters:
Step 1 : save script as w2x 
Step 2: if you are in same directory call script as w2x FileName_To beConverted
Step 3: Confirm that file doesn’t have ^M character in it now by opening it in vi editor. Important that these characters are only visible in Vi can can’t be seen through more or cat. 
To call it from anywhere: you need to make small entry in .profile or .login or .cshrc file which is generally hidden and can’t be seen simply. you can use  (ls -a) to see hidden files.
Request you to share through comments if you are aware of any other method  to remove M characters (^M) in UNIX.
Thanks 

Related Topics:

  1. Executing PMCMD command on UNIX and Syntax

4 Comments

Leave a Reply



nine − = 4

Garcinia cambogia reviews

Wins and Fails Compilation

New Vine Compilation

Muscle Maximizer Review

Crazy Vine Compilation

Auto binary signals

Most Motivational Video Ever

Funny Cats Pics Part 1

Crazy car crashes compilation

Black and White Cats Pics

Most Inspirational Video Ever

Stunts Fails Compilation

Cute Kittens

Garcinia cambogia review

gay social networking

Google Sniper 2 Review

Funny Vines

Fat Cats in Pics

Funny & Cute Cats Compilation

Cute Cat Funny Cat Compilation

Blue Eyed Cats Pics

Funny Animal Videos

Abyssinians Cats in Pics

Scared Cat Compilation

Healthy diet

Cute Kittens in Pics

Wet Cats

Cute Kittens Pics

Best Wins and Fails Compilation

Smiling Cats Pics

Best Vines Compilation

Funny Video

Motorcycle Fails Compilation

Funny Vine Compilation

Talking Cats

Cat Fight in Pics

Huge Vines Compilation

Amazing VINES Compilation

Cats in Clothes Pics

The Ultimate Herpes Protocol Review

Baby Laughing Compilation

Kitty fights against sleep

Green Eyed Cats Pics

Funny Vine Compilation

Fail Compilation

Collection of scary ghost videos

Best Vines

viral script

cat pics

Flying Cats in Pics

Scary Car crashes accident compilation

Funny Cats Compilation

Automatic Binary Options Review