Tag Archives: The Linux foundation

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

    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/

Solution to shell script error “expr: syntax error”

A few days ago, I wrote a shell script, which was normally run on my local test server. In the online server environment, I ran syntax error near token ‘. There was no problem looking at shell script left and right, so I could not Google search. It is as follows:
Open your SHELL script file with the command VI-b, and you will. Notice that each line of script ends up with an extra ^M.
So the next thing I need to figure out is what is this to the M?
long long ago….. The old teletype machine used two characters to start a new line. A character moves the slider backward (called carriage return, < CR> , ASCII code is 0D), another character moves the paper one line (called newline, < LF> , ASCII code is 0A). When computers came along, memory used to be very expensive. Some people decide that it’s unnecessary to use two characters for the end of a line. UNIX developers decided that they could use a single character for the end of a line. Linux follows UNIX, which is also < LF> . Apple developers prescribe the use of < CR> . The guys who developed MS-DOS and Windows decided to stick with the old fashioned. CR> < LF> .
Because MS-DOS and Windows are carriage return + line feed to represent line feed, Vim is used in Linux to view the code written in Windows with VC. The “^M” symbol at the end of the line represents the character.
To solve this problem in Vim, simply use the substitution function in Vim to eliminate “^M” and type the following substitution command line:
1)vi -b setup.sh
2) At command edit line < ESC key and shift+: colon > Input: % s/M ^// g
Note: the “^M” character on the command line above is not “^” plus “M”, but is generated by “Ctrl+ V” and “Ctrl+M”.
After this substitution, the save is ready to be executed. Of course there are other alternatives such as:
A. Some Linux versions have the DOS2UNIX program, which can be used to remove ^M.
b.cat filename1 | tr -d “/r” > Newfile gets rid of ^M to create a newfile, sed commands, etc. Any command that can be replaced can be used to create a newfile.

According to the above mentioned, delete the ^Mshell script and it will work fine. Later, I asked my colleague that he had changed the program path in Windows Notepad, resulting in an extra ^M in each line.

Error connecting to master, – retry time: 60 retries: 86400

Two virtual machines configured MySQL master slave when the virtual machine input
> show slave status\G;
Displays the following error
Last_IO_Error: error connecting to master ‘[email protected]:3307’ – Retry time: 60 Retry: 86400
I. Screening:
1. The username and password are correct
2. The port number of master is correct
3. Master authorization

# cat/data/mysql2/hostname. Err “to view the error log”

Slave I/O: ERROR connecting to master ‘[email protected]:3306’ – retry-time: 60 retry: 86400, Error_code: 2013
You don’t see anything. Error messages have no value.

Mysql – uslavelaowang – h192.168.32.133 – ppasswd
ERROR 2003 (HY000): Can’t connect to MySQL Server on ‘192.168.32.133’ (113)

# perror 113
OS Error code 113: No Route to host
【 Consider network reasons 】

# ping 192.168.32.133 [from the main ping] # Ping 192.168.32.132 [from the main ping] both have the return result, indicating network connection. Iptables -Fservice Iptables Save [Master Cleaning Firewall Rules]
Slave operation

# mysql – uslavelaowang h192.168.32.133 — ppasswd

ERROR 2003 (HY000): Can’t connect to MySQL Server on ‘192.168.32.133’ (111)

# perror 111
OS Error code 111: Connection union (union (union))
# mysql [Enter mysql under]
> slave stop;
> slave start;
> show slave status;
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Return two Yes’s

Reason: Iptalbes firewall rules are not cleared.