Reputation: 17336
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
.
echo -n
for AA
separately i.e. I can't club echo AA
with echo version part
with the latter echo command.So, the resultant aa.txt
file visually looks like this:
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:
for first ^M
, the characters are \r
, cr sp
and 015 040
(i.e. carriage return and an invisible space character).
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.
Upvotes: 0
Views: 9225
Reputation: 1244
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:
:e ++ff=dos
+ :set ff=unix
If you are working on a Windows machine the following may work:
Upvotes: 2
Reputation: 5098
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.
Upvotes: 2