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

ubuntu:

sudo apt-get install dos2unix

dos2unix file


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//g ENTER 
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 enlightened
https://etllabs.com/unix/remove-m-characters-ctrl-m-unix/1944/
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值