0

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.

  • I have to use echo -n for AA separately i.e. I can't club echo AAwith echo version part with the latter echo command.

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.

snakecharmerb
  • 28,223
  • 10
  • 51
  • 86
AKS
  • 14,113
  • 34
  • 144
  • 227

2 Answers2

2

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.

Greg Schmidt
  • 4,471
  • 2
  • 10
  • 31
  • Yes, how to remove it while in a `vi` command mode. I thought there was a sequence of characters that you have to press for `vi` to catch it something like `ESCAPE+[[+M` or similar. – AKS Feb 21 '19 at 16:48
  • 1
    Yeah, as I said, the sequence is Ctrl-V Ctrl-M. Ctrl-V tells `vi` that the next character should be taken as-is instead of having it's normal function. – Greg Schmidt Feb 21 '19 at 16:54
1

Type

:%s/\r/\r/g

This is saying:

:%s Find and Replace (substitute) in the entire file (%)
\r find carriage returns
\r and replace with newlines (or replace it with nothing by leaving this out the second \r)
g multiple times in the same lines

I know it sounds stupid, but on the find part, vim includes carriage returns under \r and in the replace section vim uses newlines.

Alternatives that may work for you if the above doesn't work:

  • Type the following keys (whilst in normal mode) exactly as you see them
    :%s/Ctrl+vCtrl+m//gEnter
  • :e ++ff=dos+ :set ff=unix

If you are working on a Windows machine the following may work:

  • :%s/Ctrl+qCtrl+m//gEnter
Stun Brick
  • 872
  • 2
  • 13