Home » 2017 » August » 13 » Perl trick to remove Ctrl - M charater from a file

7:37 AM
Perl trick to remove Ctrl - M charater from a file

Hello open system guys,

we always get into situation where by we have to copy files from windows to/from Linux/UNIX/AIX syetsm. 

When this happens there is pretty much a chance that the ctrl - M character may be found in the files we receive from Window system.


If there is a control - M character in a batch or a script , the script will failed as hell saying "command not found"

The resolution is we have to remove them.

In today's article i will tell you how to do this using perl command.

I have already shared the linux/Unix command to do so at here


Simply use the below command :

perl -p -i -e 's/\r\n$/\n/g;' <filename>

The above command will work on the <filename> directly and remove any or all ctrl-M characters.in it.


It simply replaces '/r' by nothing. '/r' is a special character for ^M or ctrl - M.

g --- globally

s ---  substitite


If you like it share it..!







Category: Open System-Linux | Views: 902 | Added by: shanky | Tags: remove control M characters, perl, perl command, perl tricks | Rating: 0.0/0

Related blogs

You may also like to see:

[2019-09-27][Open System-Linux]
How to generate private/public key from PEM file With PuttyGen
[2014-09-21][Open System-Linux]
How to define command prompt strings in Linux : what are $PS1, $PS2, $PS3 and $PS4?
[2014-03-12][Open System-Linux]
uptime command in Linux
[2014-09-13][Open System-Linux]
md5sum: calculate and check md5 message digest of a file in Linux
[2014-11-25][Open System-Linux]
DMESG command in Linux : to display or control kernel ring buffer

Total comments: 0