How to Remove ^M Character
There are a lot of other systems out there other than Linux, so if you have a file from, let’s say a DOS system, with extra ^M (caret M) characters at the end, you can correct it using vi. The tough part is, you will not be able to see these extra characters immediately, unless you encounter something like this:
$ ./check_summary.pl --help
-bash: ./check_summary.pl: /usr/bin/perl^M: bad interpreter: No such file or directory
(Note: check_summary is a Nagios plugin that I am currently testing.)
See the ^M character at the end? It is called the DOS line break. Unfortunately, Linux is not able to recognize these line breaks so you have to delete them. With vim. Yes, with vim, or with any editor you want, but in this post, I will show how to do it in vi.
First, open up your file where there is an extra ^M characters. While in command mode, type the following:
:%s/^V^M//g
The ^V is a CONTROL+V character and ^M is a CONTROL+M. When you type this, it will look like this:
:%s/^M//g
This command searches for ^M character (the CONTROL+V escapes a control character) the replaces it with null. After doing this, save and exit.
Another way to fix this is to use the dos2unix command like so:
$ dos2unix [file]
This might work, but I have not tried yet. Have you tried using dos2unix? Did it work? Let me know in the comments section.
Using Perl to Replace Text in Multiple Files
You can search and replace text inside multiple files using Perl. This is how you do it:
$ perl -i -pe 's/Windows/Linux/;' test*
This command will search for the word ‘Windows’ inside all files that begin with ‘test’. When it finds one, it will substitute it with the word ‘Linux’. This is useful if you have multiple files that contain the same text. Remember that you can use regular expressions to make searching flexible.
VIM Trick: How to Comment Multiple Lines
If you need to comment out lines of text inside VIM, you can try this trick:
(Enter command mode in VIM)
:40,105s/^/#/g
This will insert the # character at the beginning of each line starting from line number 40 to line number 105.
If you cannot see the line numbers, do this:
(Enter command mode in VIM)
:set number
To remove the lines:
:set nonumber
This is quite handy if you need to comment out lines of codes in a script.
Backspace Key Not Working in PuTTY
If you access your Linux machines from a Windows workstations by SSH, most probably you are using PuTTY. My new job requires me to work with SUSE Enterprise servers and to my surprise, the VIM on SLES is somewhat different from that of RHEL.
One example is the backspace key. It just won’t work in PuTTY connecting to SUSE server. I have to put the cursor before the character I want to delete and press the DELETE key. If you have this issue with PuTTY/SUSE too, this tip might help you.
Go to your PuTTY configuration -> Terminal -> Keyboard
Look for Change Sequences Sent By -> The Backspace Key
From there, select Control-H. Save your session and try it.
This one worked for me, hope this works for you too.
Search PinoyTux
Subscribe to Email Feeds
Blog Lounge
Popular Posts
Recent Posts
Drop your Card Here
Recent Comments
- Anidich1 on Tip: Add User and Generate Password Script
- Tom S on Cebu Pacific Sucks
- kadersardar on PinoyTux Spreads Some CommentLuv
- Steve on Creative Labs Threatens Third Party Driver Modder
- Barry on Free Laptops with Broadband Connection








