Home » 2016 » October » 12 » Remove Control M characters from a script or any file in Linux/AIX

3:44 PM
Remove Control M characters from a script or any file in Linux/AIX

Hi Guys,
Welcome back,

Being a consultant in DBS project I have witnessed many cased where the shell script that is copied or created may contain some special characters. This happens when the script is copied from windows system to AIX and the script was having extra spaces.

So, if the script is having such speciall characters, it might not get executed successfully throwing error as below:

 

Command not found: ^M

 

The solution is we have to find and remove all special characters like these from the script.

Here we shall practice a very simple way to do the same. 

1. Using command tr.
Use command tr to delete all '^M' characters from the script, redirect the output to a different file. rename that file back to original file.

Example:

tr -d '\r\f' originalfile > newFile.

mv newFile originalfile.

-d option is used to delete any specified characters from the file.

 

2. Using Vi editor

Open the file in Vi editor. Use the substitute method as below

:%s/^M//g

It means search for ^M and replace it with nothing globally if the flag 'g' is given.

3. Using SED 

We can use SED command also to modify the file on the go.

Use -i option to backup the file before making any changes.

Example:

#> sed -i.bak 's/^M//g' filename

The file will be modified and a backup will also be created with .bak extension.

 

Please Like/Share/comment if you find this useful.

 
 

Category: Open System-Linux | Views: 1843 | Added by: shanky | Tags: remove control M characters, remove special character, tr | Rating: 0.0/0

Related blogs


You may also like to see:


[2014-10-04][Open System-Linux]
SYSCTL : A command to configure kernel parameters at runtime in linux
[2015-06-16][Open System-Linux]
Alien command : convert files from one form to another, install binary packages
[2014-09-20][Open System-Linux]
Export command in Linux
[2014-03-11][Open System-Linux]
STRINGS : a command in linux to read the non-text files
[2016-01-23][Open System-Linux]
Some useful examples of netstat command in Linux for network statistics

Total comments: 0
ComForm">
avatar