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

本文介绍如何在UNIX系统中处理MS-DOS文件导入时产生的M字符问题,提供了从手动操作到使用脚本自动化的方法。包括使用dos2unix命令、sed命令、vi编辑器及Unix脚本进行字符替换,同时提供了脚本示例,便于用户在不同场景下应用。

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/
分布式微服务企业级系统是一个基于Spring、SpringMVC、MyBatis和Dubbo等技术的分布式敏捷开发系统架构。该系统采用微服务架构和模块化设计,提供整套公共微服务模块,包括集中权限管理(支持单点登录)、内容管理、支付中心、用户管理(支持第三方登录)、微信平台、存储系统、配置中心、日志分析、任务和通知等功能。系统支持服务治理、监控和追踪,确保高可用性和可扩展性,适用于中小型企业的J2EE企业级开发解决方案。 该系统使用Java作为主要编程语言,结合Spring框架实现依赖注入和事务管理,SpringMVC处理Web请求,MyBatis进行数据持久化操作,Dubbo实现分布式服务调用。架构模式包括微服务架构、分布式系统架构和模块化架构,设计模式应用了单例模式、工厂模式和观察者模式,以提高代码复用性和系统稳定性。 应用场景广泛,可用于企业信息化管理、电子商务平台、社交应用开发等领域,帮助开发者快速构建高效、安全的分布式系统。本资源包含完整的源码和详细论文,适合计算机科学或软件工程专业的毕业设计参考,提供实践案例和技术文档,助力学生和开发者深入理解微服务架构和分布式系统实现。 【版权说明】源码来源于网络,遵循原项目开源协议。付费内容为本人原创论文,包含技术分析和实现思路。仅供学习交流使用。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值