What is `^M` and how do I get rid of it?

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
85
down vote

favorite
32












When I open the file in vim, I am seeing strange ^M characters.



Unfortunately, the world's favorite search engine does not do well with special characters in queries, so I'm asking here:



  • What is this ^M character?


  • How could it have gotten there?


  • How do I get rid of it?







share|improve this question





















  • Vlastimil that was a pointless edit. Got is proper past tense of get in British English.
    – Jesse_b
    Feb 9 at 3:55














up vote
85
down vote

favorite
32












When I open the file in vim, I am seeing strange ^M characters.



Unfortunately, the world's favorite search engine does not do well with special characters in queries, so I'm asking here:



  • What is this ^M character?


  • How could it have gotten there?


  • How do I get rid of it?







share|improve this question





















  • Vlastimil that was a pointless edit. Got is proper past tense of get in British English.
    – Jesse_b
    Feb 9 at 3:55












up vote
85
down vote

favorite
32









up vote
85
down vote

favorite
32






32





When I open the file in vim, I am seeing strange ^M characters.



Unfortunately, the world's favorite search engine does not do well with special characters in queries, so I'm asking here:



  • What is this ^M character?


  • How could it have gotten there?


  • How do I get rid of it?







share|improve this question













When I open the file in vim, I am seeing strange ^M characters.



Unfortunately, the world's favorite search engine does not do well with special characters in queries, so I'm asking here:



  • What is this ^M character?


  • How could it have gotten there?


  • How do I get rid of it?









share|improve this question












share|improve this question




share|improve this question








edited Feb 4 at 8:23









Vlastimil

6,2761146116




6,2761146116









asked Feb 17 '12 at 23:10









Christoph Wurm

1,73841725




1,73841725











  • Vlastimil that was a pointless edit. Got is proper past tense of get in British English.
    – Jesse_b
    Feb 9 at 3:55
















  • Vlastimil that was a pointless edit. Got is proper past tense of get in British English.
    – Jesse_b
    Feb 9 at 3:55















Vlastimil that was a pointless edit. Got is proper past tense of get in British English.
– Jesse_b
Feb 9 at 3:55




Vlastimil that was a pointless edit. Got is proper past tense of get in British English.
– Jesse_b
Feb 9 at 3:55










11 Answers
11






active

oldest

votes

















up vote
70
down vote



accepted










The ^M is a carriage-return character. If you see this, you're probably looking at a file that originated in the DOS/Windows world, where an end-of-line is marked by a carriage return/newline pair, whereas in the Unix world, end-of-line is marked by a single newline.



Read this article for more detail, and also the Wikipedia entry for newline.



This article discusses how to set up vim to transparently edit files with different end-of-line markers.



If you have a file with ^M at the end of some lines and you want to get rid of them, use this in Vim:



:s/^M$//


(Press Ctrl+V Ctrl+M to insert that ^M.)






share|improve this answer



















  • 7




    Try :%s/^M/r/g instead to remove ^M and replace ^M with newline character r. Without %, the command applies for current line only. And I came across some examples where ^M is not at end of line, such as The first line.^MThe second line.
    – George
    Apr 14 '15 at 4:29











  • Or if you don't want loads of line breaks you could just do :%s/^M/
    – carefulnow1
    Nov 21 '16 at 8:52






  • 1




    If it's just a carriage return by itself, that might be the classic (pre-Unix) Macintosh line break. Even some newer programs like Excel 2007 for Mac do that for some reason.
    – sudo
    Apr 18 '17 at 17:22











  • @larsks But altap.cz/salamander/help/salamand/appendix_txtfiles says that ^M is used in UNIX
    – Vivek
    Feb 16 at 7:23











  • The article to which you have linked says that n is used in Unix, which is correct. That is ASCII code 10, otherwise known as ^J.
    – larsks
    Feb 16 at 21:05

















up vote
34
down vote













Most UNIX operating systems have a utility called dos2unix that will convert the CRLF to LF. The other answers cover the "what are they" question.






share|improve this answer




























    up vote
    27
    down vote













    A simpler way to do this is to use the following command:



    dos2unix filename


    This command works with path patterns as well, Eg



    dos2unix path/name*


    If it doesn't work, try using different mode:



    dos2unix -c mac filename



    • -c Set conversion mode. Where CONVMODE is one of: ascii, 7bit, iso, mac with ascii being the default.





    share|improve this answer






























      up vote
      8
      down vote













      This worked for me



      :e ++ff=dos 


      then



      :set ff=unix 


      and finally



      :wq 





      share|improve this answer





















      • thanks! this worked for me but the accepted solution didnt
        – Mike Palmice
        May 30 at 18:03

















      up vote
      8
      down vote













      Another way to get rid of carriage returns is with the tr command.



      I have a small script that look like this



      #!/bin/sh
      tmpfile=$(mktemp)
      tr -d 'r' <"$1" >"$tmpfile"
      mv "$tmpfile" "$1"





      share|improve this answer






























        up vote
        6
        down vote













        You can clean this up with sed:



        sed -e 's/^M$//' < infile > outfile


        The trick is how to enter the carriage-return properly. Generally, you need to type C-v C-m to enter a literal carriage return. You can also have sed work in place with



        sed -i.bak -e 's/^M$//' infile





        share|improve this answer




























          up vote
          2
          down vote













          In my case,



          Nothing above worked, I had a CSV file copied to Linux machine from my mac and I used all the above commands but nothing helped but the below one



          tr "15" "n" < inputfile > outputfile


          I had a file in which ^M characters were sandwitched between lines something like below



          Audi,A4,35 TFSi Premium,,CAAUA4TP^MB01BNKT6TG,TRO_WBFB_500,Trico,CARS,Audi,A4,35 TFSi Premium,,CAAUA4TP^MB01BNKTG0A,TRO_WB_T500,Trico,





          share|improve this answer




























            up vote
            1
            down vote













            What is this ^M?

            The ^M is a carriage-return character. If you see this, you're probably looking at a file that originated in the DOS/Windows world, where an end-of-line is marked by a carriage return/newline pair, whereas in the Unix world, end-of-line is marked by a single newline.



            How could it have got there?

            When there is change in file format.



            How do I get rid of it?

            open your file with



            vim -b FILE_PATH


            save it with following command



            :%s/^M//g





            share|improve this answer























            • You've got a typo in open your filr with.
              – Mateusz Piotrowski
              Jan 27 '16 at 20:48






            • 2




              This answer does not add anything to the other answers. The first paragraph is an almost verbatim copy from the accepted answer. The given code will not save anything, but just remove all carriage return characters from all lines. And I am not sure how opening the file in binary mode will help here.
              – Dubu
              Jan 28 '16 at 7:58


















            up vote
            0
            down vote













            You can use Vim in Ex mode:



            ex -bsc '%s/r//|x' file


            1. -b binary mode


            2. % select all lines


            3. s substitute


            4. r carriage return


            5. x save and close






            share|improve this answer




























              up vote
              0
              down vote













              In the past, I have seen even configuration files are not parsed properly and complain about whitespace, but if you vi and do a set list it won't show the whitespace, grep filename [[space]] will show you ^M



              that's when dos2unix file helps






              share|improve this answer






























                up vote
                0
                down vote













                None of the options here worked for me.



                Finally opened the file in MS Word, and voila the ^M disappeared. Cut and paste to any other editor of your choice.






                share|improve this answer























                • How much time did it take you to do that compared to a command while you're already in the file, imagine you're working with a large group of file. You'd be wasting a lot of time! Also depend on the file size, it is going to be cumbersome,.
                  – amrx
                  Jul 15 at 0:39









                protected by Community♦ Aug 1 '17 at 9:46



                Thank you for your interest in this question.
                Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).



                Would you like to answer one of these unanswered questions instead?














                11 Answers
                11






                active

                oldest

                votes








                11 Answers
                11






                active

                oldest

                votes









                active

                oldest

                votes






                active

                oldest

                votes








                up vote
                70
                down vote



                accepted










                The ^M is a carriage-return character. If you see this, you're probably looking at a file that originated in the DOS/Windows world, where an end-of-line is marked by a carriage return/newline pair, whereas in the Unix world, end-of-line is marked by a single newline.



                Read this article for more detail, and also the Wikipedia entry for newline.



                This article discusses how to set up vim to transparently edit files with different end-of-line markers.



                If you have a file with ^M at the end of some lines and you want to get rid of them, use this in Vim:



                :s/^M$//


                (Press Ctrl+V Ctrl+M to insert that ^M.)






                share|improve this answer



















                • 7




                  Try :%s/^M/r/g instead to remove ^M and replace ^M with newline character r. Without %, the command applies for current line only. And I came across some examples where ^M is not at end of line, such as The first line.^MThe second line.
                  – George
                  Apr 14 '15 at 4:29











                • Or if you don't want loads of line breaks you could just do :%s/^M/
                  – carefulnow1
                  Nov 21 '16 at 8:52






                • 1




                  If it's just a carriage return by itself, that might be the classic (pre-Unix) Macintosh line break. Even some newer programs like Excel 2007 for Mac do that for some reason.
                  – sudo
                  Apr 18 '17 at 17:22











                • @larsks But altap.cz/salamander/help/salamand/appendix_txtfiles says that ^M is used in UNIX
                  – Vivek
                  Feb 16 at 7:23











                • The article to which you have linked says that n is used in Unix, which is correct. That is ASCII code 10, otherwise known as ^J.
                  – larsks
                  Feb 16 at 21:05














                up vote
                70
                down vote



                accepted










                The ^M is a carriage-return character. If you see this, you're probably looking at a file that originated in the DOS/Windows world, where an end-of-line is marked by a carriage return/newline pair, whereas in the Unix world, end-of-line is marked by a single newline.



                Read this article for more detail, and also the Wikipedia entry for newline.



                This article discusses how to set up vim to transparently edit files with different end-of-line markers.



                If you have a file with ^M at the end of some lines and you want to get rid of them, use this in Vim:



                :s/^M$//


                (Press Ctrl+V Ctrl+M to insert that ^M.)






                share|improve this answer



















                • 7




                  Try :%s/^M/r/g instead to remove ^M and replace ^M with newline character r. Without %, the command applies for current line only. And I came across some examples where ^M is not at end of line, such as The first line.^MThe second line.
                  – George
                  Apr 14 '15 at 4:29











                • Or if you don't want loads of line breaks you could just do :%s/^M/
                  – carefulnow1
                  Nov 21 '16 at 8:52






                • 1




                  If it's just a carriage return by itself, that might be the classic (pre-Unix) Macintosh line break. Even some newer programs like Excel 2007 for Mac do that for some reason.
                  – sudo
                  Apr 18 '17 at 17:22











                • @larsks But altap.cz/salamander/help/salamand/appendix_txtfiles says that ^M is used in UNIX
                  – Vivek
                  Feb 16 at 7:23











                • The article to which you have linked says that n is used in Unix, which is correct. That is ASCII code 10, otherwise known as ^J.
                  – larsks
                  Feb 16 at 21:05












                up vote
                70
                down vote



                accepted







                up vote
                70
                down vote



                accepted






                The ^M is a carriage-return character. If you see this, you're probably looking at a file that originated in the DOS/Windows world, where an end-of-line is marked by a carriage return/newline pair, whereas in the Unix world, end-of-line is marked by a single newline.



                Read this article for more detail, and also the Wikipedia entry for newline.



                This article discusses how to set up vim to transparently edit files with different end-of-line markers.



                If you have a file with ^M at the end of some lines and you want to get rid of them, use this in Vim:



                :s/^M$//


                (Press Ctrl+V Ctrl+M to insert that ^M.)






                share|improve this answer















                The ^M is a carriage-return character. If you see this, you're probably looking at a file that originated in the DOS/Windows world, where an end-of-line is marked by a carriage return/newline pair, whereas in the Unix world, end-of-line is marked by a single newline.



                Read this article for more detail, and also the Wikipedia entry for newline.



                This article discusses how to set up vim to transparently edit files with different end-of-line markers.



                If you have a file with ^M at the end of some lines and you want to get rid of them, use this in Vim:



                :s/^M$//


                (Press Ctrl+V Ctrl+M to insert that ^M.)







                share|improve this answer















                share|improve this answer



                share|improve this answer








                edited Feb 20 '12 at 2:58









                Gilles

                503k1179941519




                503k1179941519











                answered Feb 17 '12 at 23:14









                larsks

                9,50132738




                9,50132738







                • 7




                  Try :%s/^M/r/g instead to remove ^M and replace ^M with newline character r. Without %, the command applies for current line only. And I came across some examples where ^M is not at end of line, such as The first line.^MThe second line.
                  – George
                  Apr 14 '15 at 4:29











                • Or if you don't want loads of line breaks you could just do :%s/^M/
                  – carefulnow1
                  Nov 21 '16 at 8:52






                • 1




                  If it's just a carriage return by itself, that might be the classic (pre-Unix) Macintosh line break. Even some newer programs like Excel 2007 for Mac do that for some reason.
                  – sudo
                  Apr 18 '17 at 17:22











                • @larsks But altap.cz/salamander/help/salamand/appendix_txtfiles says that ^M is used in UNIX
                  – Vivek
                  Feb 16 at 7:23











                • The article to which you have linked says that n is used in Unix, which is correct. That is ASCII code 10, otherwise known as ^J.
                  – larsks
                  Feb 16 at 21:05












                • 7




                  Try :%s/^M/r/g instead to remove ^M and replace ^M with newline character r. Without %, the command applies for current line only. And I came across some examples where ^M is not at end of line, such as The first line.^MThe second line.
                  – George
                  Apr 14 '15 at 4:29











                • Or if you don't want loads of line breaks you could just do :%s/^M/
                  – carefulnow1
                  Nov 21 '16 at 8:52






                • 1




                  If it's just a carriage return by itself, that might be the classic (pre-Unix) Macintosh line break. Even some newer programs like Excel 2007 for Mac do that for some reason.
                  – sudo
                  Apr 18 '17 at 17:22











                • @larsks But altap.cz/salamander/help/salamand/appendix_txtfiles says that ^M is used in UNIX
                  – Vivek
                  Feb 16 at 7:23











                • The article to which you have linked says that n is used in Unix, which is correct. That is ASCII code 10, otherwise known as ^J.
                  – larsks
                  Feb 16 at 21:05







                7




                7




                Try :%s/^M/r/g instead to remove ^M and replace ^M with newline character r. Without %, the command applies for current line only. And I came across some examples where ^M is not at end of line, such as The first line.^MThe second line.
                – George
                Apr 14 '15 at 4:29





                Try :%s/^M/r/g instead to remove ^M and replace ^M with newline character r. Without %, the command applies for current line only. And I came across some examples where ^M is not at end of line, such as The first line.^MThe second line.
                – George
                Apr 14 '15 at 4:29













                Or if you don't want loads of line breaks you could just do :%s/^M/
                – carefulnow1
                Nov 21 '16 at 8:52




                Or if you don't want loads of line breaks you could just do :%s/^M/
                – carefulnow1
                Nov 21 '16 at 8:52




                1




                1




                If it's just a carriage return by itself, that might be the classic (pre-Unix) Macintosh line break. Even some newer programs like Excel 2007 for Mac do that for some reason.
                – sudo
                Apr 18 '17 at 17:22





                If it's just a carriage return by itself, that might be the classic (pre-Unix) Macintosh line break. Even some newer programs like Excel 2007 for Mac do that for some reason.
                – sudo
                Apr 18 '17 at 17:22













                @larsks But altap.cz/salamander/help/salamand/appendix_txtfiles says that ^M is used in UNIX
                – Vivek
                Feb 16 at 7:23





                @larsks But altap.cz/salamander/help/salamand/appendix_txtfiles says that ^M is used in UNIX
                – Vivek
                Feb 16 at 7:23













                The article to which you have linked says that n is used in Unix, which is correct. That is ASCII code 10, otherwise known as ^J.
                – larsks
                Feb 16 at 21:05




                The article to which you have linked says that n is used in Unix, which is correct. That is ASCII code 10, otherwise known as ^J.
                – larsks
                Feb 16 at 21:05












                up vote
                34
                down vote













                Most UNIX operating systems have a utility called dos2unix that will convert the CRLF to LF. The other answers cover the "what are they" question.






                share|improve this answer

























                  up vote
                  34
                  down vote













                  Most UNIX operating systems have a utility called dos2unix that will convert the CRLF to LF. The other answers cover the "what are they" question.






                  share|improve this answer























                    up vote
                    34
                    down vote










                    up vote
                    34
                    down vote









                    Most UNIX operating systems have a utility called dos2unix that will convert the CRLF to LF. The other answers cover the "what are they" question.






                    share|improve this answer













                    Most UNIX operating systems have a utility called dos2unix that will convert the CRLF to LF. The other answers cover the "what are they" question.







                    share|improve this answer













                    share|improve this answer



                    share|improve this answer











                    answered Feb 18 '12 at 3:14









                    Aaron Brown

                    1,00577




                    1,00577




















                        up vote
                        27
                        down vote













                        A simpler way to do this is to use the following command:



                        dos2unix filename


                        This command works with path patterns as well, Eg



                        dos2unix path/name*


                        If it doesn't work, try using different mode:



                        dos2unix -c mac filename



                        • -c Set conversion mode. Where CONVMODE is one of: ascii, 7bit, iso, mac with ascii being the default.





                        share|improve this answer



























                          up vote
                          27
                          down vote













                          A simpler way to do this is to use the following command:



                          dos2unix filename


                          This command works with path patterns as well, Eg



                          dos2unix path/name*


                          If it doesn't work, try using different mode:



                          dos2unix -c mac filename



                          • -c Set conversion mode. Where CONVMODE is one of: ascii, 7bit, iso, mac with ascii being the default.





                          share|improve this answer

























                            up vote
                            27
                            down vote










                            up vote
                            27
                            down vote









                            A simpler way to do this is to use the following command:



                            dos2unix filename


                            This command works with path patterns as well, Eg



                            dos2unix path/name*


                            If it doesn't work, try using different mode:



                            dos2unix -c mac filename



                            • -c Set conversion mode. Where CONVMODE is one of: ascii, 7bit, iso, mac with ascii being the default.





                            share|improve this answer















                            A simpler way to do this is to use the following command:



                            dos2unix filename


                            This command works with path patterns as well, Eg



                            dos2unix path/name*


                            If it doesn't work, try using different mode:



                            dos2unix -c mac filename



                            • -c Set conversion mode. Where CONVMODE is one of: ascii, 7bit, iso, mac with ascii being the default.






                            share|improve this answer















                            share|improve this answer



                            share|improve this answer








                            edited Nov 13 '17 at 16:01









                            Devaldo

                            32




                            32











                            answered Feb 23 '12 at 17:30









                            AnonGeek

                            37124




                            37124




















                                up vote
                                8
                                down vote













                                This worked for me



                                :e ++ff=dos 


                                then



                                :set ff=unix 


                                and finally



                                :wq 





                                share|improve this answer





















                                • thanks! this worked for me but the accepted solution didnt
                                  – Mike Palmice
                                  May 30 at 18:03














                                up vote
                                8
                                down vote













                                This worked for me



                                :e ++ff=dos 


                                then



                                :set ff=unix 


                                and finally



                                :wq 





                                share|improve this answer





















                                • thanks! this worked for me but the accepted solution didnt
                                  – Mike Palmice
                                  May 30 at 18:03












                                up vote
                                8
                                down vote










                                up vote
                                8
                                down vote









                                This worked for me



                                :e ++ff=dos 


                                then



                                :set ff=unix 


                                and finally



                                :wq 





                                share|improve this answer













                                This worked for me



                                :e ++ff=dos 


                                then



                                :set ff=unix 


                                and finally



                                :wq 






                                share|improve this answer













                                share|improve this answer



                                share|improve this answer











                                answered Dec 6 '16 at 20:43









                                Stryker

                                19112




                                19112











                                • thanks! this worked for me but the accepted solution didnt
                                  – Mike Palmice
                                  May 30 at 18:03
















                                • thanks! this worked for me but the accepted solution didnt
                                  – Mike Palmice
                                  May 30 at 18:03















                                thanks! this worked for me but the accepted solution didnt
                                – Mike Palmice
                                May 30 at 18:03




                                thanks! this worked for me but the accepted solution didnt
                                – Mike Palmice
                                May 30 at 18:03










                                up vote
                                8
                                down vote













                                Another way to get rid of carriage returns is with the tr command.



                                I have a small script that look like this



                                #!/bin/sh
                                tmpfile=$(mktemp)
                                tr -d 'r' <"$1" >"$tmpfile"
                                mv "$tmpfile" "$1"





                                share|improve this answer



























                                  up vote
                                  8
                                  down vote













                                  Another way to get rid of carriage returns is with the tr command.



                                  I have a small script that look like this



                                  #!/bin/sh
                                  tmpfile=$(mktemp)
                                  tr -d 'r' <"$1" >"$tmpfile"
                                  mv "$tmpfile" "$1"





                                  share|improve this answer

























                                    up vote
                                    8
                                    down vote










                                    up vote
                                    8
                                    down vote









                                    Another way to get rid of carriage returns is with the tr command.



                                    I have a small script that look like this



                                    #!/bin/sh
                                    tmpfile=$(mktemp)
                                    tr -d 'r' <"$1" >"$tmpfile"
                                    mv "$tmpfile" "$1"





                                    share|improve this answer















                                    Another way to get rid of carriage returns is with the tr command.



                                    I have a small script that look like this



                                    #!/bin/sh
                                    tmpfile=$(mktemp)
                                    tr -d 'r' <"$1" >"$tmpfile"
                                    mv "$tmpfile" "$1"






                                    share|improve this answer















                                    share|improve this answer



                                    share|improve this answer








                                    edited Feb 4 at 9:19









                                    Kusalananda

                                    101k13199312




                                    101k13199312











                                    answered Feb 18 '12 at 21:27









                                    Johan

                                    3,09911627




                                    3,09911627




















                                        up vote
                                        6
                                        down vote













                                        You can clean this up with sed:



                                        sed -e 's/^M$//' < infile > outfile


                                        The trick is how to enter the carriage-return properly. Generally, you need to type C-v C-m to enter a literal carriage return. You can also have sed work in place with



                                        sed -i.bak -e 's/^M$//' infile





                                        share|improve this answer

























                                          up vote
                                          6
                                          down vote













                                          You can clean this up with sed:



                                          sed -e 's/^M$//' < infile > outfile


                                          The trick is how to enter the carriage-return properly. Generally, you need to type C-v C-m to enter a literal carriage return. You can also have sed work in place with



                                          sed -i.bak -e 's/^M$//' infile





                                          share|improve this answer























                                            up vote
                                            6
                                            down vote










                                            up vote
                                            6
                                            down vote









                                            You can clean this up with sed:



                                            sed -e 's/^M$//' < infile > outfile


                                            The trick is how to enter the carriage-return properly. Generally, you need to type C-v C-m to enter a literal carriage return. You can also have sed work in place with



                                            sed -i.bak -e 's/^M$//' infile





                                            share|improve this answer













                                            You can clean this up with sed:



                                            sed -e 's/^M$//' < infile > outfile


                                            The trick is how to enter the carriage-return properly. Generally, you need to type C-v C-m to enter a literal carriage return. You can also have sed work in place with



                                            sed -i.bak -e 's/^M$//' infile






                                            share|improve this answer













                                            share|improve this answer



                                            share|improve this answer











                                            answered Feb 18 '12 at 5:07









                                            Dale Hagglund

                                            31612




                                            31612




















                                                up vote
                                                2
                                                down vote













                                                In my case,



                                                Nothing above worked, I had a CSV file copied to Linux machine from my mac and I used all the above commands but nothing helped but the below one



                                                tr "15" "n" < inputfile > outputfile


                                                I had a file in which ^M characters were sandwitched between lines something like below



                                                Audi,A4,35 TFSi Premium,,CAAUA4TP^MB01BNKT6TG,TRO_WBFB_500,Trico,CARS,Audi,A4,35 TFSi Premium,,CAAUA4TP^MB01BNKTG0A,TRO_WB_T500,Trico,





                                                share|improve this answer

























                                                  up vote
                                                  2
                                                  down vote













                                                  In my case,



                                                  Nothing above worked, I had a CSV file copied to Linux machine from my mac and I used all the above commands but nothing helped but the below one



                                                  tr "15" "n" < inputfile > outputfile


                                                  I had a file in which ^M characters were sandwitched between lines something like below



                                                  Audi,A4,35 TFSi Premium,,CAAUA4TP^MB01BNKT6TG,TRO_WBFB_500,Trico,CARS,Audi,A4,35 TFSi Premium,,CAAUA4TP^MB01BNKTG0A,TRO_WB_T500,Trico,





                                                  share|improve this answer























                                                    up vote
                                                    2
                                                    down vote










                                                    up vote
                                                    2
                                                    down vote









                                                    In my case,



                                                    Nothing above worked, I had a CSV file copied to Linux machine from my mac and I used all the above commands but nothing helped but the below one



                                                    tr "15" "n" < inputfile > outputfile


                                                    I had a file in which ^M characters were sandwitched between lines something like below



                                                    Audi,A4,35 TFSi Premium,,CAAUA4TP^MB01BNKT6TG,TRO_WBFB_500,Trico,CARS,Audi,A4,35 TFSi Premium,,CAAUA4TP^MB01BNKTG0A,TRO_WB_T500,Trico,





                                                    share|improve this answer













                                                    In my case,



                                                    Nothing above worked, I had a CSV file copied to Linux machine from my mac and I used all the above commands but nothing helped but the below one



                                                    tr "15" "n" < inputfile > outputfile


                                                    I had a file in which ^M characters were sandwitched between lines something like below



                                                    Audi,A4,35 TFSi Premium,,CAAUA4TP^MB01BNKT6TG,TRO_WBFB_500,Trico,CARS,Audi,A4,35 TFSi Premium,,CAAUA4TP^MB01BNKTG0A,TRO_WB_T500,Trico,






                                                    share|improve this answer













                                                    share|improve this answer



                                                    share|improve this answer











                                                    answered Dec 7 '16 at 8:45









                                                    Vishwanath gowda k

                                                    32538




                                                    32538




















                                                        up vote
                                                        1
                                                        down vote













                                                        What is this ^M?

                                                        The ^M is a carriage-return character. If you see this, you're probably looking at a file that originated in the DOS/Windows world, where an end-of-line is marked by a carriage return/newline pair, whereas in the Unix world, end-of-line is marked by a single newline.



                                                        How could it have got there?

                                                        When there is change in file format.



                                                        How do I get rid of it?

                                                        open your file with



                                                        vim -b FILE_PATH


                                                        save it with following command



                                                        :%s/^M//g





                                                        share|improve this answer























                                                        • You've got a typo in open your filr with.
                                                          – Mateusz Piotrowski
                                                          Jan 27 '16 at 20:48






                                                        • 2




                                                          This answer does not add anything to the other answers. The first paragraph is an almost verbatim copy from the accepted answer. The given code will not save anything, but just remove all carriage return characters from all lines. And I am not sure how opening the file in binary mode will help here.
                                                          – Dubu
                                                          Jan 28 '16 at 7:58















                                                        up vote
                                                        1
                                                        down vote













                                                        What is this ^M?

                                                        The ^M is a carriage-return character. If you see this, you're probably looking at a file that originated in the DOS/Windows world, where an end-of-line is marked by a carriage return/newline pair, whereas in the Unix world, end-of-line is marked by a single newline.



                                                        How could it have got there?

                                                        When there is change in file format.



                                                        How do I get rid of it?

                                                        open your file with



                                                        vim -b FILE_PATH


                                                        save it with following command



                                                        :%s/^M//g





                                                        share|improve this answer























                                                        • You've got a typo in open your filr with.
                                                          – Mateusz Piotrowski
                                                          Jan 27 '16 at 20:48






                                                        • 2




                                                          This answer does not add anything to the other answers. The first paragraph is an almost verbatim copy from the accepted answer. The given code will not save anything, but just remove all carriage return characters from all lines. And I am not sure how opening the file in binary mode will help here.
                                                          – Dubu
                                                          Jan 28 '16 at 7:58













                                                        up vote
                                                        1
                                                        down vote










                                                        up vote
                                                        1
                                                        down vote









                                                        What is this ^M?

                                                        The ^M is a carriage-return character. If you see this, you're probably looking at a file that originated in the DOS/Windows world, where an end-of-line is marked by a carriage return/newline pair, whereas in the Unix world, end-of-line is marked by a single newline.



                                                        How could it have got there?

                                                        When there is change in file format.



                                                        How do I get rid of it?

                                                        open your file with



                                                        vim -b FILE_PATH


                                                        save it with following command



                                                        :%s/^M//g





                                                        share|improve this answer















                                                        What is this ^M?

                                                        The ^M is a carriage-return character. If you see this, you're probably looking at a file that originated in the DOS/Windows world, where an end-of-line is marked by a carriage return/newline pair, whereas in the Unix world, end-of-line is marked by a single newline.



                                                        How could it have got there?

                                                        When there is change in file format.



                                                        How do I get rid of it?

                                                        open your file with



                                                        vim -b FILE_PATH


                                                        save it with following command



                                                        :%s/^M//g






                                                        share|improve this answer















                                                        share|improve this answer



                                                        share|improve this answer








                                                        edited Jan 28 '16 at 7:25


























                                                        answered Nov 21 '15 at 16:45









                                                        Prashant Kanse

                                                        1193




                                                        1193











                                                        • You've got a typo in open your filr with.
                                                          – Mateusz Piotrowski
                                                          Jan 27 '16 at 20:48






                                                        • 2




                                                          This answer does not add anything to the other answers. The first paragraph is an almost verbatim copy from the accepted answer. The given code will not save anything, but just remove all carriage return characters from all lines. And I am not sure how opening the file in binary mode will help here.
                                                          – Dubu
                                                          Jan 28 '16 at 7:58

















                                                        • You've got a typo in open your filr with.
                                                          – Mateusz Piotrowski
                                                          Jan 27 '16 at 20:48






                                                        • 2




                                                          This answer does not add anything to the other answers. The first paragraph is an almost verbatim copy from the accepted answer. The given code will not save anything, but just remove all carriage return characters from all lines. And I am not sure how opening the file in binary mode will help here.
                                                          – Dubu
                                                          Jan 28 '16 at 7:58
















                                                        You've got a typo in open your filr with.
                                                        – Mateusz Piotrowski
                                                        Jan 27 '16 at 20:48




                                                        You've got a typo in open your filr with.
                                                        – Mateusz Piotrowski
                                                        Jan 27 '16 at 20:48




                                                        2




                                                        2




                                                        This answer does not add anything to the other answers. The first paragraph is an almost verbatim copy from the accepted answer. The given code will not save anything, but just remove all carriage return characters from all lines. And I am not sure how opening the file in binary mode will help here.
                                                        – Dubu
                                                        Jan 28 '16 at 7:58





                                                        This answer does not add anything to the other answers. The first paragraph is an almost verbatim copy from the accepted answer. The given code will not save anything, but just remove all carriage return characters from all lines. And I am not sure how opening the file in binary mode will help here.
                                                        – Dubu
                                                        Jan 28 '16 at 7:58











                                                        up vote
                                                        0
                                                        down vote













                                                        You can use Vim in Ex mode:



                                                        ex -bsc '%s/r//|x' file


                                                        1. -b binary mode


                                                        2. % select all lines


                                                        3. s substitute


                                                        4. r carriage return


                                                        5. x save and close






                                                        share|improve this answer

























                                                          up vote
                                                          0
                                                          down vote













                                                          You can use Vim in Ex mode:



                                                          ex -bsc '%s/r//|x' file


                                                          1. -b binary mode


                                                          2. % select all lines


                                                          3. s substitute


                                                          4. r carriage return


                                                          5. x save and close






                                                          share|improve this answer























                                                            up vote
                                                            0
                                                            down vote










                                                            up vote
                                                            0
                                                            down vote









                                                            You can use Vim in Ex mode:



                                                            ex -bsc '%s/r//|x' file


                                                            1. -b binary mode


                                                            2. % select all lines


                                                            3. s substitute


                                                            4. r carriage return


                                                            5. x save and close






                                                            share|improve this answer













                                                            You can use Vim in Ex mode:



                                                            ex -bsc '%s/r//|x' file


                                                            1. -b binary mode


                                                            2. % select all lines


                                                            3. s substitute


                                                            4. r carriage return


                                                            5. x save and close







                                                            share|improve this answer













                                                            share|improve this answer



                                                            share|improve this answer











                                                            answered Apr 17 '16 at 5:06









                                                            Steven Penny

                                                            2,31821535




                                                            2,31821535




















                                                                up vote
                                                                0
                                                                down vote













                                                                In the past, I have seen even configuration files are not parsed properly and complain about whitespace, but if you vi and do a set list it won't show the whitespace, grep filename [[space]] will show you ^M



                                                                that's when dos2unix file helps






                                                                share|improve this answer



























                                                                  up vote
                                                                  0
                                                                  down vote













                                                                  In the past, I have seen even configuration files are not parsed properly and complain about whitespace, but if you vi and do a set list it won't show the whitespace, grep filename [[space]] will show you ^M



                                                                  that's when dos2unix file helps






                                                                  share|improve this answer

























                                                                    up vote
                                                                    0
                                                                    down vote










                                                                    up vote
                                                                    0
                                                                    down vote









                                                                    In the past, I have seen even configuration files are not parsed properly and complain about whitespace, but if you vi and do a set list it won't show the whitespace, grep filename [[space]] will show you ^M



                                                                    that's when dos2unix file helps






                                                                    share|improve this answer















                                                                    In the past, I have seen even configuration files are not parsed properly and complain about whitespace, but if you vi and do a set list it won't show the whitespace, grep filename [[space]] will show you ^M



                                                                    that's when dos2unix file helps







                                                                    share|improve this answer















                                                                    share|improve this answer



                                                                    share|improve this answer








                                                                    edited Jul 10 '17 at 2:45









                                                                    andrew lorien

                                                                    17010




                                                                    17010











                                                                    answered Mar 29 '17 at 18:57









                                                                    Sriram

                                                                    92




                                                                    92




















                                                                        up vote
                                                                        0
                                                                        down vote













                                                                        None of the options here worked for me.



                                                                        Finally opened the file in MS Word, and voila the ^M disappeared. Cut and paste to any other editor of your choice.






                                                                        share|improve this answer























                                                                        • How much time did it take you to do that compared to a command while you're already in the file, imagine you're working with a large group of file. You'd be wasting a lot of time! Also depend on the file size, it is going to be cumbersome,.
                                                                          – amrx
                                                                          Jul 15 at 0:39














                                                                        up vote
                                                                        0
                                                                        down vote













                                                                        None of the options here worked for me.



                                                                        Finally opened the file in MS Word, and voila the ^M disappeared. Cut and paste to any other editor of your choice.






                                                                        share|improve this answer























                                                                        • How much time did it take you to do that compared to a command while you're already in the file, imagine you're working with a large group of file. You'd be wasting a lot of time! Also depend on the file size, it is going to be cumbersome,.
                                                                          – amrx
                                                                          Jul 15 at 0:39












                                                                        up vote
                                                                        0
                                                                        down vote










                                                                        up vote
                                                                        0
                                                                        down vote









                                                                        None of the options here worked for me.



                                                                        Finally opened the file in MS Word, and voila the ^M disappeared. Cut and paste to any other editor of your choice.






                                                                        share|improve this answer















                                                                        None of the options here worked for me.



                                                                        Finally opened the file in MS Word, and voila the ^M disappeared. Cut and paste to any other editor of your choice.







                                                                        share|improve this answer















                                                                        share|improve this answer



                                                                        share|improve this answer








                                                                        edited Jul 17 '17 at 5:17









                                                                        Anthon

                                                                        58.2k1794157




                                                                        58.2k1794157











                                                                        answered Mar 3 '17 at 19:31









                                                                        user219042

                                                                        1




                                                                        1











                                                                        • How much time did it take you to do that compared to a command while you're already in the file, imagine you're working with a large group of file. You'd be wasting a lot of time! Also depend on the file size, it is going to be cumbersome,.
                                                                          – amrx
                                                                          Jul 15 at 0:39
















                                                                        • How much time did it take you to do that compared to a command while you're already in the file, imagine you're working with a large group of file. You'd be wasting a lot of time! Also depend on the file size, it is going to be cumbersome,.
                                                                          – amrx
                                                                          Jul 15 at 0:39















                                                                        How much time did it take you to do that compared to a command while you're already in the file, imagine you're working with a large group of file. You'd be wasting a lot of time! Also depend on the file size, it is going to be cumbersome,.
                                                                        – amrx
                                                                        Jul 15 at 0:39




                                                                        How much time did it take you to do that compared to a command while you're already in the file, imagine you're working with a large group of file. You'd be wasting a lot of time! Also depend on the file size, it is going to be cumbersome,.
                                                                        – amrx
                                                                        Jul 15 at 0:39





                                                                        protected by Community♦ Aug 1 '17 at 9:46



                                                                        Thank you for your interest in this question.
                                                                        Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).



                                                                        Would you like to answer one of these unanswered questions instead?


                                                                        Popular posts from this blog

                                                                        How to check contact read email or not when send email to Individual?

                                                                        Bahrain

                                                                        Postfix configuration issue with fips on centos 7; mailgun relay