Replace a ^M character in Vi
Solved
guin
-
leon91490 Posted messages 200 Registration date Status Member Last intervention -
leon91490 Posted messages 200 Registration date Status Member Last intervention -
J have a lot of ^M characters in my Vi file under Linux
do you have a way to get rid of them
I tried :1;$ s/\^M//g but it doesn't work!!
Thank you for your help
do you have a way to get rid of them
I tried :1;$ s/\^M//g but it doesn't work!!
Thank you for your help
9 answers
I'm responding to this old post because it ranks well on Google and the answers are quite light...
In short, just type in vim:
:set fileformat=unix
and the file is converted with Unix line endings.
Conversely:
:set fileformat=dos
will allow Windows users to read your file comfortably. This command sets the file format, so there’s no need to do it again each time you open it.
The Unix command
file my_file.txt
allows you to know the type of my_file.txt (or any file with valid mime content)
And indeed, with the tofrodos package installed, you have the commands:
fromdos my_file.txt
todos my_file.txt
which have quite explicit names.
In the same category in vim:
:set fileencoding=utf-8
converts the iso-something or latinX file to utf8
Conversely:
:set fileencoding=iso-8859-15
converts the file to be readable on win98 or for your outdated website ]:->
On the command line, "iconv" will do wonders. For more precision:
man iconv
In short, just type in vim:
:set fileformat=unix
and the file is converted with Unix line endings.
Conversely:
:set fileformat=dos
will allow Windows users to read your file comfortably. This command sets the file format, so there’s no need to do it again each time you open it.
The Unix command
file my_file.txt
allows you to know the type of my_file.txt (or any file with valid mime content)
And indeed, with the tofrodos package installed, you have the commands:
fromdos my_file.txt
todos my_file.txt
which have quite explicit names.
In the same category in vim:
:set fileencoding=utf-8
converts the iso-something or latinX file to utf8
Conversely:
:set fileencoding=iso-8859-15
converts the file to be readable on win98 or for your outdated website ]:->
On the command line, "iconv" will do wonders. For more precision:
man iconv
jj
super not useful when vim is not installed
Hello Guin,
To complete the answer, in "vi", you can type non-printable characters like "^M" (which means "Ctrl-M", this combination gives the ASCII code equivalent to the "enter" key) by pressing "Ctrl-v" followed by your non-printable key.
So by typing Ctrl-v then Ctrl-M, you get a "^M" understood by vi as the non-printable character Ctrl-M.
Dal
To complete the answer, in "vi", you can type non-printable characters like "^M" (which means "Ctrl-M", this combination gives the ASCII code equivalent to the "enter" key) by pressing "Ctrl-v" followed by your non-printable key.
So by typing Ctrl-v then Ctrl-M, you get a "^M" understood by vi as the non-printable character Ctrl-M.
Dal
Hello,
For inexplicable reasons, dos2unix (or fromdos) is ineffective on this file filled with ^M and that is just one huge line!
To replace ^M with an actual line break, you can do:
:% s/^M/\r/g (where ^M is obtained as mentioned above: Ctrl V + Ctrl M )
but
:% s/\r/\r/g works just as well!!! (vim 7 on Linux Debian etch)
(the reasons might be that a mac2unix was needed?)
For inexplicable reasons, dos2unix (or fromdos) is ineffective on this file filled with ^M and that is just one huge line!
To replace ^M with an actual line break, you can do:
:% s/^M/\r/g (where ^M is obtained as mentioned above: Ctrl V + Ctrl M )
but
:% s/\r/\r/g works just as well!!! (vim 7 on Linux Debian etch)
(the reasons might be that a mac2unix was needed?)
Hello,
(the reasons might be that a mac2unix was needed?)
I don't think so ;-)
(the reasons might be that a mac2unix was needed?)
I don't think so ;-)
lami20j@debian:~$ cat -v CR line1^M line2^M lami20j@debian:~$ perl -pi -e 's/\r//' CR lami20j@debian:~$ cat -v CR line1 line2 lami20j@debian:~$ perl -pi -e 's/\n/\r\n/' CR lami20j@debian:~$ cat -v CR line1^M line2^M lami20j@debian:~$ sed -i 's/\r//' CR lami20j@debian:~$ cat -v CR line1 line2
Hello
Very interesting, but my file was formatted with just one very long line, full of ^M
( this is the sommaire.html file from the SPIP template: IENSP-XvPP which can be found here: http://www.stephane-kus.fr/IMG/zip/IENSP-XvPP-3-1.zip )
thank you
Very interesting, but my file was formatted with just one very long line, full of ^M
( this is the sommaire.html file from the SPIP template: IENSP-XvPP which can be found here: http://www.stephane-kus.fr/IMG/zip/IENSP-XvPP-3-1.zip )
thank you
Thank you lami20, but with this solution, while we do successfully eliminate the numerous ^M, we do not replace them with line breaks as we would like! They are just removed.
perl -pi.orig -e 's/^M/\r/g' sommaire.html
perl -pi.orig -e 's/\r/\r/g' sommaire.html
have no effect (unlike the substitution in vim which faithfully provided this service for me).
perl -pi.orig -e 's/^M/\r/g' sommaire.html
perl -pi.orig -e 's/\r/\r/g' sommaire.html
have no effect (unlike the substitution in vim which faithfully provided this service for me).
Hello,
The Unix command
file mon_fichier.txt
allows you to know the type of mon_fichier.txt
Additionally, the cat command allows you to see non-printable characters
106485010510997108
The Unix command
file mon_fichier.txt
allows you to know the type of mon_fichier.txt
Additionally, the cat command allows you to see non-printable characters
cat -A fichier.txt--
106485010510997108
Hi,
The file in question was it previously stored under Windows?
I had a somewhat similar problem (line breaks with a \M or L, I can't remember the letter), a script that I had stored under Windows and read with vi under Linux.
I used the 'dos2unix' program
Then the script was like new :)
Luc L.
[Gentoo] finally :Þ
The file in question was it previously stored under Windows?
I had a somewhat similar problem (line breaks with a \M or L, I can't remember the letter), a script that I had stored under Windows and read with vi under Linux.
I used the 'dos2unix' program
Then the script was like new :)
Luc L.
[Gentoo] finally :Þ
voui, I subscribe, although at my place the machine is called fromdos
fromdos < yourdisgustingfile > yourcleanfile
but just a note regarding your replacement in vi: control characters, you don't type them with a hat (even escaped) followed by the character. to get your "hat-M" you type Ctrl-V Ctrl-M
all control characters are typed like this, Ctrl-V followed by your control character
which results in (but I'm not sure this will appear... oh well no, it doesn't appear)
one in the fields
you’ll have it nasty
fromdos < yourdisgustingfile > yourcleanfile
but just a note regarding your replacement in vi: control characters, you don't type them with a hat (even escaped) followed by the character. to get your "hat-M" you type Ctrl-V Ctrl-M
all control characters are typed like this, Ctrl-V followed by your control character
which results in (but I'm not sure this will appear... oh well no, it doesn't appear)
one in the fields
you’ll have it nasty
Hello,
we don't replace them with line breaks as we would like
It's normal since that's what I executed and I must have misunderstood that this is what you actually want ;-)
Otherwise
And if you want to keep the DOS format
we don't replace them with line breaks as we would like
It's normal since that's what I executed and I must have misunderstood that this is what you actually want ;-)
Otherwise
lami20j@debian:~$ cat -v fic_ line1^Mline2^Mline3^Mline4^M lami20j@debian:~$ perl -pi.orig -e 's/\r/\n/g' fic_ lami20j@debian:~$ cat -v fic_ line1 line2 line3 line4 lami20j@debian:~$ cat -v fic_.orig line1^Mline2^Mline3^Mline4^M
And if you want to keep the DOS format
lami20j@debian:~$ cat -v fic_ line1^Mline2^Mline3^Mline4^M lami20j@debian:~$ perl -pi.orig -e 's/\r/\r\n/g' fic_ lami20j@debian:~$ cat -v fic_ line1^M line2^M line3^M line4^M lami20j@debian:~$ cat -v fic_.orig line1^Mline2^Mline3^Mline4^M
It seems simple to describe:
My file is just a single line, stuffed with ^M
I want each ^M displayed to become a real new line
Among all these methods, only the one I mentioned worked on this file.
I am using Debian Etch, Gnome, and regardless of the encoding mode of the GNOME Terminal (v 2.14.2), none (iso-1 iso-15 or UTF-8) solves this problem (usually under UTF-8 and in French)
Did you go see/test the file in question?
Its description by file is as follows:
file sommaire.html: Non-ISO extended-ASCII HTML document text, with CR line terminators
I think the "non-iso" must refer to this..
My file is just a single line, stuffed with ^M
I want each ^M displayed to become a real new line
Among all these methods, only the one I mentioned worked on this file.
I am using Debian Etch, Gnome, and regardless of the encoding mode of the GNOME Terminal (v 2.14.2), none (iso-1 iso-15 or UTF-8) solves this problem (usually under UTF-8 and in French)
Did you go see/test the file in question?
Its description by file is as follows:
file sommaire.html: Non-ISO extended-ASCII HTML document text, with CR line terminators
I think the "non-iso" must refer to this..
Hi,
Did you go see/test the file in question?
Of course I did;-(
I tested the replacement command that I wrote on your files (the one you gave me in message 10) and it works.
I don't know why you couldn't get it to work.
To please you, I just tested it once again.
Here’s what I have
Did you go see/test the file in question?
Of course I did;-(
I tested the replacement command that I wrote on your files (the one you gave me in message 10) and it works.
I don't know why you couldn't get it to work.
To please you, I just tested it once again.
Here’s what I have
$ file sommaire.html sommaire.html: Non-ISO extended-ASCII HTML document text, with CR line terminators $ perl -pi.orig -e 's/\r/\n/g' sommaire.html $ file sommaire.html sommaire.html: Non-ISO extended-ASCII HTML document text $ file sommaire.html.orig sommaire.html.orig: Non-ISO extended-ASCII HTML document text, with CR line terminators $ mv sommaire.html.orig sommaire.html $ file sommaire.html sommaire.html: Non-ISO extended-ASCII HTML document text, with CR line terminators $ perl -pi.orig -e 's/\r/\r\n/g' sommaire.html $ file sommaire.html sommaire.html: Non-ISO extended-ASCII HTML document text, with CRLF line terminators $ file sommaire.html.orig sommaire.html.orig: Non-ISO extended-ASCII HTML document text, with CR line terminators $