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/
Read More:
- Solve syntax error: unexpected end of file or /bin/bash^m: bad interpeneter: no match file or directory
- PHP big file upload problem (500m or above)
- When the MAC M1 uiautomatorviewer is opened, it displays blank or reports an error
- Extract audio from ffmpeg video to mp3 format or m4a format command
- Mac bigsur cannot create other directory solutions in the root directory (M1 chip is also applicable)
- Solution of node canvas error reporting in MacOS M1 installation
- Redis compilation error cannot find config.m4
- Solve the problem that M1 Mac can’t debug when using GoLand
- The method of constructing even order magic square (n = 4 * m)
- Solution to flash back of MacBook Pro open software of M1 chip
- MacBook M1 Big Sur logging into forticlient SSL VPN
- [Solved] Unity Error: Assertion failed on expression: ‘m_ErrorCode == MDB_MAP_RESIZED
- Error: (22,57) Java: type parameter… Mapper is not in the range of type variable M
- Remove spaces after characters in Excel cells
- Error using ones Size inputs must be integers. Error in testdisplay1 (line 38) array=-ones(buf+m*(s
- onnx-tensorrt/builtin_op_importers.cpp:766:12: error: ‘class nvinfer1::IDeconvolutionLayer’ has no m
- Lenovo x3650 M5 installation esxi-5.5.0u3 failed to resolve circular relocation
- Solve the problem of using logback console to print log Chinese characters in IDEA as garbled characters
- Caused by: org.flywaydb.core.api.FlywayException: Validate failed: Migration checksum mismatch for m
- Spring configuration error: org.springframework.beans . factory.BeanCreationException : Error creating bean with name ‘m