grepechospecial-charactersvidos2unix

How to remove ^M special character in VI mode - Can't remove using dos2unix, can't print the whole line doing cat or grep


Linux: RHEL5 Tikanga Version: 2.6.18-402.el5 (per uname -a)

I'm creating a file by grepping some line from a windows source file (which contains some Version=x.x.x value) and putting it to new file aa.txt. This works fine for finding and grepping part.

Doing it something like echo -n "AA "; echo "$(grep "Version=[1-9]" /some/mounted/loc/windows/sourcefile.txt) -" >> aa.txt.

So, the resultant aa.txt file visually looks like this: enter image description here Now, if I cat it, it doesn't print the first line, just the last - character.

If I use dos2unix (which actually works for removing the line ending character only and converts a Windows format file to a Linux line ending character format file), it's still not working as ^M in first line is not the last character in the line. PS: If you notice, the color of first ^M is different than the second line one, which I added manually by typing.

Running od -cab aa.txt (octal dump) on this file, shows me that:

  1. for first ^M, the characters are \r, cr sp and 015 040 (i.e. carriage return and an invisible space character).

  2. for the second line, ^M, is showing me normal values as ^ M, ^ M and 136 115 (as I manually typed ^ and M in the second line, this was expected).

Question:
1. Using tr -d '\r' < aa.txt > new.aa.txt I can solve this issue but what pattern I can use within vi or vim to remove this character in command mode. Tried :%s/^M//g but it didn't catch the pattern.


Solution

  • You presumably used an actual caret followed by an M? The character there is actually a Ctrl-M, which you can get with Ctrl-V followed by Ctrl-M.