Deleting/Removing Control M characters in Linux
Control-M is a character found at the end of a line usually in files in linux/unix. We need to delete these characters before processing this file.
To put a control m character, please Press Cntrl followed by V and M(Control-V+Control-M) and not as Shift-6-M.
Viewing the control M characters we can use below commands:
1) cat -v filename
2) vi -b filename
To Remove Control M characters we can use below methods:
1. dos2unix The simplest of all is using the dos2unix command.
dos2unix filename
Note: I have seen occurences where the this command dont remove all Control-M characters so I prefer below menthod
2. tr Command
$ tr -d '^M' <filename
To store output after deleting the Control-M we can use below
tr -d '^M' <filename > newfile
We can also use below
$ tr -d "\015" <filename >newfile
3. sed command
$ sed -e 's/^M//g' filename
To remove and save changes to file use below
$ sed -i 's/^M//g' filename
4. vi Editor in the escape mode type below:
:1,%s/^M//g
5. awk command
The 1 is used to print the lines.
$ awk 'sub(/^M/,"");1' filename
Post a Comment
Post a Comment