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
- Why I am seeing ^M (control M ) character in UNIX file ? How to make my UNIX script \ file clean and without ^M (control M) character?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 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
https://etllabs.com/unix/remove-m-characters-ctrl-m-unix/1944/