Remove carriage return in Unix












186















What is the simplest way to remove all the carriage returns r from a file in Unix?










share|improve this question




















  • 2





    Are you talking about either 'r' 'n', or just the nasty 'r's?

    – v3.
    Apr 28 '09 at 22:10











  • Related: grep to find files that contain ^M (Windows carriage return).

    – user456814
    May 15 '14 at 21:17
















186















What is the simplest way to remove all the carriage returns r from a file in Unix?










share|improve this question




















  • 2





    Are you talking about either 'r' 'n', or just the nasty 'r's?

    – v3.
    Apr 28 '09 at 22:10











  • Related: grep to find files that contain ^M (Windows carriage return).

    – user456814
    May 15 '14 at 21:17














186












186








186


52






What is the simplest way to remove all the carriage returns r from a file in Unix?










share|improve this question
















What is the simplest way to remove all the carriage returns r from a file in Unix?







unix carriage-return






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jul 10 '13 at 7:54









devnull

83.6k21157181




83.6k21157181










asked Apr 28 '09 at 22:05









AldurAldur

1,17041213




1,17041213








  • 2





    Are you talking about either 'r' 'n', or just the nasty 'r's?

    – v3.
    Apr 28 '09 at 22:10











  • Related: grep to find files that contain ^M (Windows carriage return).

    – user456814
    May 15 '14 at 21:17














  • 2





    Are you talking about either 'r' 'n', or just the nasty 'r's?

    – v3.
    Apr 28 '09 at 22:10











  • Related: grep to find files that contain ^M (Windows carriage return).

    – user456814
    May 15 '14 at 21:17








2




2





Are you talking about either 'r' 'n', or just the nasty 'r's?

– v3.
Apr 28 '09 at 22:10





Are you talking about either 'r' 'n', or just the nasty 'r's?

– v3.
Apr 28 '09 at 22:10













Related: grep to find files that contain ^M (Windows carriage return).

– user456814
May 15 '14 at 21:17





Related: grep to find files that contain ^M (Windows carriage return).

– user456814
May 15 '14 at 21:17












16 Answers
16






active

oldest

votes


















231














I'm going to assume you mean carriage returns (CR, "r", 0x0d) at the ends of lines rather than just blindly within a file (you may have them in the middle of strings for all I know). Using this test file with a CR at the end of the first line only:



$ cat infile
hello
goodbye

$ cat infile | od -c
0000000 h e l l o r n g o o d b y e n
0000017


dos2unix is the way to go if it's installed on your system:



$ cat infile | dos2unix -U | od -c
0000000 h e l l o n g o o d b y e n
0000016


If for some reason dos2unix is not available to you, then sed will do it:



$ cat infile | sed 's/r$//' | od -c
0000000 h e l l o n g o o d b y e n
0000016


If for some reason sed is not available to you, then ed will do it, in a complicated way:



$ echo ',s/rn/n/
> w !cat
> Q' | ed infile 2>/dev/null | od -c
0000000 h e l l o n g o o d b y e n
0000016


If you don't have any of those tools installed on your box, you've got bigger problems than trying to convert files :-)






share|improve this answer





















  • 11





    r works only with GNU sed, else you can do this: sed `echo "s/r//"`

    – lapo
    Feb 24 '11 at 16:47








  • 14





    Neither sed nor echo recognise r on MacOs. In this case only printf "r" appears to work.

    – Steve Powell
    Feb 6 '12 at 16:04






  • 26





    To elaborate on @steve's comment: On a Mac, use the following: sed "s/$(printf 'r')$//"

    – mklement0
    May 8 '12 at 21:35








  • 7





    To fix issue on mac you can also prefix the single-quote sed string with $ like so: sed $'s@r@@g' |od -c (but if you would replace with n you would need to escape it)

    – nhed
    Apr 12 '13 at 17:25








  • 1





    I'm not 100% sure, but for OS X, using CTRL-V + CTRL-M in place of r looks like it might work.

    – user456814
    May 15 '14 at 21:44



















205














tr -d 'r' < infile > outfile


See tr(1)






share|improve this answer



















  • 1





    Very simple and reliable. thank you.

    – maček
    Jul 8 '14 at 21:46






  • 4





    Not great: 1. doesn't work inplace, 2. can replace r also not at EOL (which may or may not be what you want...).

    – Tomasz Gandor
    Jul 9 '14 at 10:33






  • 4





    1. Most unixy tools work that way, and it's usually the safest way to go about things since if you screw up you still have the original. 2. The question as stated is to remove carriage returns, not to convert line endings. But there are plenty of other answers that might serve you better.

    – Henrik Gustafsson
    Jul 9 '14 at 11:56






  • 1





    If your tr does not support the r escape, try '15' or perhaps a literal '^M' (in many shells on many terminals, ctrl-V ctrl-M will produce a literal ctrl-M character).

    – tripleee
    Aug 25 '14 at 10:55








  • 1





    This is what works on AIX.

    – Jesse Chisholm
    Dec 30 '15 at 21:09



















34














Old School:



tr -d 'r' < filewithcarriagereturns > filewithoutcarriagereturns





share|improve this answer



















  • 1





    This worked like charm :) Thanks.

    – user1336087
    Oct 29 '14 at 15:36








  • 3





    Just don't use the same file as the destination...

    – Hejazzman
    Dec 29 '17 at 8:13



















27














There's a utility called dos2unix that exists on many systems, and can be easily installed on most.






share|improve this answer



















  • 6





    Sometimes it is also called fromdos (and todos).

    – Anonymous
    Apr 29 '09 at 13:59



















17














The simplest way on Linux is, in my humble opinion,



sed -i 's/r//g' <filename>


The strong quotes around the substitution operator 's/r//' are essential. Without them the shell will interpret r as an escape+r and reduce it to a plain r, and remove all lower case r. That's why the answer given above in 2009 by Rob doesn't work.



And adding the /g modifier ensures that even multiple r will be removed, and not only the first one.






share|improve this answer

































    7














    sed -i s/r// <filename> or somesuch; see man sed or the wealth of information available on the web regarding use of sed.



    One thing to point out is the precise meaning of "carriage return" in the above; if you truly mean the single control character "carriage return", then the pattern above is correct. If you meant, more generally, CRLF (carriage return and a line feed, which is how line feeds are implemented under Windows), then you probably want to replace rn instead. Bare line feeds (newline) in Linux/Unix are n.






    share|improve this answer
























    • I am trying to use --> sed 's/rn/=/' countryNew.txt > demo.txt which does not work. "tiger" "Lion."

      – Suvasis
      Sep 13 '13 at 7:12













    • are we to take that to mean you're on a mac? I've noticed Darwin sed seems to have different commands and feature sets by default than most Linux versions...

      – jsh
      Jan 23 '14 at 17:51






    • 4





      FYI, the s/r// doesn't seem to remove carriage returns on OS X, it seems to remove literal r chars instead. I'm not sure why that is yet. Maybe it has something to do with the way the string is quoted? As a workaround, using CTRL-V + CTRL-M in place of r seems to work.

      – user456814
      May 15 '14 at 21:38



















    6














    If you are a Vi user, you may open the file and remove the carriage return with:



    :%s/r//g


    or with



    :1,$ s/^M//


    Note that you should type ^M by pressing ctrl-v and then ctrl-m.






    share|improve this answer



















    • 2





      Not great: if the file has CR on every line (i.e. is a correct DOS file), vim will load it with filetype=dos, and not show ^M-s at all. Getting around this is a ton of keystrokes, which is not what vim is made for ;). I'd just go for sed -i, and then `-e 's/r$//g' to limit the removal to CRs at EOL.

      – Tomasz Gandor
      Jul 9 '14 at 10:35



















    6














    Once more a solution... Because there's always one more:



    perl -i -pe 's/r//' filename


    It's nice because it's in place and works in every flavor of unix/linux I've worked with.






    share|improve this answer

































      3














      Someone else recommend dos2unix and I strongly recommend it as well. I'm just providing more details.



      If installed, jump to the next step. If not already installed, I would recommend installing it via yum like:



      yum install dos2unix


      Then you can use it like:



      dos2unix fileIWantToRemoveWindowsReturnsFrom.txt





      share|improve this answer































        2














        Here is the thing,



        %0d is the carriage return character. To make it compatabile with Unix. We need to use the below command.



        dos2unix fileName.extension fileName.extension






        share|improve this answer































          1














          try this to convert dos file into unix file:




          fromdos file







          share|improve this answer































            1














            If you're using an OS (like OS X) that doesn't have the dos2unix command but does have a Python interpreter (version 2.5+), this command is equivalent to the dos2unix command:



            python -c "import sys; import fileinput; sys.stdout.writelines(line.replace('r', 'n') for line in fileinput.input(mode='rU'))"


            This handles both named files on the command line as well as pipes and redirects, just like dos2unix. If you add this line to your ~/.bashrc file (or equivalent profile file for other shells):



            alias dos2unix="python -c "import sys; import fileinput; sys.stdout.writelines(line.replace('r', 'n') for line in fileinput.input(mode='rU'))""


            ... the next time you log in (or run source ~/.bashrc in the current session) you will be able to use the dos2unix name on the command line in the same manner as in the other examples.






            share|improve this answer

































              1














              For UNIX... I've noticed dos2unix removed Unicode headers form my UTF-8 file. Under git bash (Windows), the following script seems to work nicely. It uses sed. Note it only removes carriage-returns at the ends of lines, and preserves Unicode headers.



              #!/bin/bash

              inOutFile="$1"
              backupFile="${inOutFile}~"
              mv --verbose "$inOutFile" "$backupFile"
              sed -e 's/15$//g' <"$backupFile" >"$inOutFile"





              share|improve this answer































                1














                If you are running an X environment and have a proper editor (visual studio code), then I would follow the reccomendation:



                Visual Studio Code: How to show line endings



                Just go to the bottom right corner of your screen, visual studio code will show you both the file encoding and the end of line convention followed by the file, an just with a simple click you can switch that around.



                Just use visual code as your replacement for notepad++ on a linux environment and you are set to go.






                share|improve this answer































                  0














                  I've used python for it, here my code;



                  end1='/home/.../file1.txt'
                  end2='/home/.../file2.txt'
                  with open(end1, "rb") as inf:
                  with open(end2, "w") as fixed:
                  for line in inf:
                  line = line.replace("n", "")
                  line = line.replace("r", "")
                  fixed.write(line)





                  share|improve this answer































                    -1














                    you can simply do this :



                    $ echo $(cat input) > output





                    share|improve this answer
























                    • Don't know why someone gave '-1'. This is a perfectly good answer (and the only one which worked for me).

                      – FractalSpace
                      Jun 22 '15 at 16:43






                    • 1





                      Oh, sorry, it was me. Wait, look, it really does not work for 'r'!

                      – Viacheslav Rodionov
                      Jun 25 '15 at 13:21






                    • 1





                      @FractalSpace This is a terrible idea! It completely wrecks all the spacing in the file and leaves all the contents of the file subject to interpretation by the shell. Try it with a file that contains one line a * b...

                      – Tom Fenech
                      Jan 28 '16 at 10:37











                    Your Answer






                    StackExchange.ifUsing("editor", function () {
                    StackExchange.using("externalEditor", function () {
                    StackExchange.using("snippets", function () {
                    StackExchange.snippets.init();
                    });
                    });
                    }, "code-snippets");

                    StackExchange.ready(function() {
                    var channelOptions = {
                    tags: "".split(" "),
                    id: "1"
                    };
                    initTagRenderer("".split(" "), "".split(" "), channelOptions);

                    StackExchange.using("externalEditor", function() {
                    // Have to fire editor after snippets, if snippets enabled
                    if (StackExchange.settings.snippets.snippetsEnabled) {
                    StackExchange.using("snippets", function() {
                    createEditor();
                    });
                    }
                    else {
                    createEditor();
                    }
                    });

                    function createEditor() {
                    StackExchange.prepareEditor({
                    heartbeatType: 'answer',
                    autoActivateHeartbeat: false,
                    convertImagesToLinks: true,
                    noModals: true,
                    showLowRepImageUploadWarning: true,
                    reputationToPostImages: 10,
                    bindNavPrevention: true,
                    postfix: "",
                    imageUploader: {
                    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
                    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
                    allowUrls: true
                    },
                    onDemand: true,
                    discardSelector: ".discard-answer"
                    ,immediatelyShowMarkdownHelp:true
                    });


                    }
                    });














                    draft saved

                    draft discarded


















                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f800030%2fremove-carriage-return-in-unix%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest















                    Required, but never shown

























                    16 Answers
                    16






                    active

                    oldest

                    votes








                    16 Answers
                    16






                    active

                    oldest

                    votes









                    active

                    oldest

                    votes






                    active

                    oldest

                    votes









                    231














                    I'm going to assume you mean carriage returns (CR, "r", 0x0d) at the ends of lines rather than just blindly within a file (you may have them in the middle of strings for all I know). Using this test file with a CR at the end of the first line only:



                    $ cat infile
                    hello
                    goodbye

                    $ cat infile | od -c
                    0000000 h e l l o r n g o o d b y e n
                    0000017


                    dos2unix is the way to go if it's installed on your system:



                    $ cat infile | dos2unix -U | od -c
                    0000000 h e l l o n g o o d b y e n
                    0000016


                    If for some reason dos2unix is not available to you, then sed will do it:



                    $ cat infile | sed 's/r$//' | od -c
                    0000000 h e l l o n g o o d b y e n
                    0000016


                    If for some reason sed is not available to you, then ed will do it, in a complicated way:



                    $ echo ',s/rn/n/
                    > w !cat
                    > Q' | ed infile 2>/dev/null | od -c
                    0000000 h e l l o n g o o d b y e n
                    0000016


                    If you don't have any of those tools installed on your box, you've got bigger problems than trying to convert files :-)






                    share|improve this answer





















                    • 11





                      r works only with GNU sed, else you can do this: sed `echo "s/r//"`

                      – lapo
                      Feb 24 '11 at 16:47








                    • 14





                      Neither sed nor echo recognise r on MacOs. In this case only printf "r" appears to work.

                      – Steve Powell
                      Feb 6 '12 at 16:04






                    • 26





                      To elaborate on @steve's comment: On a Mac, use the following: sed "s/$(printf 'r')$//"

                      – mklement0
                      May 8 '12 at 21:35








                    • 7





                      To fix issue on mac you can also prefix the single-quote sed string with $ like so: sed $'s@r@@g' |od -c (but if you would replace with n you would need to escape it)

                      – nhed
                      Apr 12 '13 at 17:25








                    • 1





                      I'm not 100% sure, but for OS X, using CTRL-V + CTRL-M in place of r looks like it might work.

                      – user456814
                      May 15 '14 at 21:44
















                    231














                    I'm going to assume you mean carriage returns (CR, "r", 0x0d) at the ends of lines rather than just blindly within a file (you may have them in the middle of strings for all I know). Using this test file with a CR at the end of the first line only:



                    $ cat infile
                    hello
                    goodbye

                    $ cat infile | od -c
                    0000000 h e l l o r n g o o d b y e n
                    0000017


                    dos2unix is the way to go if it's installed on your system:



                    $ cat infile | dos2unix -U | od -c
                    0000000 h e l l o n g o o d b y e n
                    0000016


                    If for some reason dos2unix is not available to you, then sed will do it:



                    $ cat infile | sed 's/r$//' | od -c
                    0000000 h e l l o n g o o d b y e n
                    0000016


                    If for some reason sed is not available to you, then ed will do it, in a complicated way:



                    $ echo ',s/rn/n/
                    > w !cat
                    > Q' | ed infile 2>/dev/null | od -c
                    0000000 h e l l o n g o o d b y e n
                    0000016


                    If you don't have any of those tools installed on your box, you've got bigger problems than trying to convert files :-)






                    share|improve this answer





















                    • 11





                      r works only with GNU sed, else you can do this: sed `echo "s/r//"`

                      – lapo
                      Feb 24 '11 at 16:47








                    • 14





                      Neither sed nor echo recognise r on MacOs. In this case only printf "r" appears to work.

                      – Steve Powell
                      Feb 6 '12 at 16:04






                    • 26





                      To elaborate on @steve's comment: On a Mac, use the following: sed "s/$(printf 'r')$//"

                      – mklement0
                      May 8 '12 at 21:35








                    • 7





                      To fix issue on mac you can also prefix the single-quote sed string with $ like so: sed $'s@r@@g' |od -c (but if you would replace with n you would need to escape it)

                      – nhed
                      Apr 12 '13 at 17:25








                    • 1





                      I'm not 100% sure, but for OS X, using CTRL-V + CTRL-M in place of r looks like it might work.

                      – user456814
                      May 15 '14 at 21:44














                    231












                    231








                    231







                    I'm going to assume you mean carriage returns (CR, "r", 0x0d) at the ends of lines rather than just blindly within a file (you may have them in the middle of strings for all I know). Using this test file with a CR at the end of the first line only:



                    $ cat infile
                    hello
                    goodbye

                    $ cat infile | od -c
                    0000000 h e l l o r n g o o d b y e n
                    0000017


                    dos2unix is the way to go if it's installed on your system:



                    $ cat infile | dos2unix -U | od -c
                    0000000 h e l l o n g o o d b y e n
                    0000016


                    If for some reason dos2unix is not available to you, then sed will do it:



                    $ cat infile | sed 's/r$//' | od -c
                    0000000 h e l l o n g o o d b y e n
                    0000016


                    If for some reason sed is not available to you, then ed will do it, in a complicated way:



                    $ echo ',s/rn/n/
                    > w !cat
                    > Q' | ed infile 2>/dev/null | od -c
                    0000000 h e l l o n g o o d b y e n
                    0000016


                    If you don't have any of those tools installed on your box, you've got bigger problems than trying to convert files :-)






                    share|improve this answer















                    I'm going to assume you mean carriage returns (CR, "r", 0x0d) at the ends of lines rather than just blindly within a file (you may have them in the middle of strings for all I know). Using this test file with a CR at the end of the first line only:



                    $ cat infile
                    hello
                    goodbye

                    $ cat infile | od -c
                    0000000 h e l l o r n g o o d b y e n
                    0000017


                    dos2unix is the way to go if it's installed on your system:



                    $ cat infile | dos2unix -U | od -c
                    0000000 h e l l o n g o o d b y e n
                    0000016


                    If for some reason dos2unix is not available to you, then sed will do it:



                    $ cat infile | sed 's/r$//' | od -c
                    0000000 h e l l o n g o o d b y e n
                    0000016


                    If for some reason sed is not available to you, then ed will do it, in a complicated way:



                    $ echo ',s/rn/n/
                    > w !cat
                    > Q' | ed infile 2>/dev/null | od -c
                    0000000 h e l l o n g o o d b y e n
                    0000016


                    If you don't have any of those tools installed on your box, you've got bigger problems than trying to convert files :-)







                    share|improve this answer














                    share|improve this answer



                    share|improve this answer








                    edited Nov 23 '17 at 1:45

























                    answered Apr 29 '09 at 2:25









                    paxdiablopaxdiablo

                    631k16912431666




                    631k16912431666








                    • 11





                      r works only with GNU sed, else you can do this: sed `echo "s/r//"`

                      – lapo
                      Feb 24 '11 at 16:47








                    • 14





                      Neither sed nor echo recognise r on MacOs. In this case only printf "r" appears to work.

                      – Steve Powell
                      Feb 6 '12 at 16:04






                    • 26





                      To elaborate on @steve's comment: On a Mac, use the following: sed "s/$(printf 'r')$//"

                      – mklement0
                      May 8 '12 at 21:35








                    • 7





                      To fix issue on mac you can also prefix the single-quote sed string with $ like so: sed $'s@r@@g' |od -c (but if you would replace with n you would need to escape it)

                      – nhed
                      Apr 12 '13 at 17:25








                    • 1





                      I'm not 100% sure, but for OS X, using CTRL-V + CTRL-M in place of r looks like it might work.

                      – user456814
                      May 15 '14 at 21:44














                    • 11





                      r works only with GNU sed, else you can do this: sed `echo "s/r//"`

                      – lapo
                      Feb 24 '11 at 16:47








                    • 14





                      Neither sed nor echo recognise r on MacOs. In this case only printf "r" appears to work.

                      – Steve Powell
                      Feb 6 '12 at 16:04






                    • 26





                      To elaborate on @steve's comment: On a Mac, use the following: sed "s/$(printf 'r')$//"

                      – mklement0
                      May 8 '12 at 21:35








                    • 7





                      To fix issue on mac you can also prefix the single-quote sed string with $ like so: sed $'s@r@@g' |od -c (but if you would replace with n you would need to escape it)

                      – nhed
                      Apr 12 '13 at 17:25








                    • 1





                      I'm not 100% sure, but for OS X, using CTRL-V + CTRL-M in place of r looks like it might work.

                      – user456814
                      May 15 '14 at 21:44








                    11




                    11





                    r works only with GNU sed, else you can do this: sed `echo "s/r//"`

                    – lapo
                    Feb 24 '11 at 16:47







                    r works only with GNU sed, else you can do this: sed `echo "s/r//"`

                    – lapo
                    Feb 24 '11 at 16:47






                    14




                    14





                    Neither sed nor echo recognise r on MacOs. In this case only printf "r" appears to work.

                    – Steve Powell
                    Feb 6 '12 at 16:04





                    Neither sed nor echo recognise r on MacOs. In this case only printf "r" appears to work.

                    – Steve Powell
                    Feb 6 '12 at 16:04




                    26




                    26





                    To elaborate on @steve's comment: On a Mac, use the following: sed "s/$(printf 'r')$//"

                    – mklement0
                    May 8 '12 at 21:35







                    To elaborate on @steve's comment: On a Mac, use the following: sed "s/$(printf 'r')$//"

                    – mklement0
                    May 8 '12 at 21:35






                    7




                    7





                    To fix issue on mac you can also prefix the single-quote sed string with $ like so: sed $'s@r@@g' |od -c (but if you would replace with n you would need to escape it)

                    – nhed
                    Apr 12 '13 at 17:25







                    To fix issue on mac you can also prefix the single-quote sed string with $ like so: sed $'s@r@@g' |od -c (but if you would replace with n you would need to escape it)

                    – nhed
                    Apr 12 '13 at 17:25






                    1




                    1





                    I'm not 100% sure, but for OS X, using CTRL-V + CTRL-M in place of r looks like it might work.

                    – user456814
                    May 15 '14 at 21:44





                    I'm not 100% sure, but for OS X, using CTRL-V + CTRL-M in place of r looks like it might work.

                    – user456814
                    May 15 '14 at 21:44













                    205














                    tr -d 'r' < infile > outfile


                    See tr(1)






                    share|improve this answer



















                    • 1





                      Very simple and reliable. thank you.

                      – maček
                      Jul 8 '14 at 21:46






                    • 4





                      Not great: 1. doesn't work inplace, 2. can replace r also not at EOL (which may or may not be what you want...).

                      – Tomasz Gandor
                      Jul 9 '14 at 10:33






                    • 4





                      1. Most unixy tools work that way, and it's usually the safest way to go about things since if you screw up you still have the original. 2. The question as stated is to remove carriage returns, not to convert line endings. But there are plenty of other answers that might serve you better.

                      – Henrik Gustafsson
                      Jul 9 '14 at 11:56






                    • 1





                      If your tr does not support the r escape, try '15' or perhaps a literal '^M' (in many shells on many terminals, ctrl-V ctrl-M will produce a literal ctrl-M character).

                      – tripleee
                      Aug 25 '14 at 10:55








                    • 1





                      This is what works on AIX.

                      – Jesse Chisholm
                      Dec 30 '15 at 21:09
















                    205














                    tr -d 'r' < infile > outfile


                    See tr(1)






                    share|improve this answer



















                    • 1





                      Very simple and reliable. thank you.

                      – maček
                      Jul 8 '14 at 21:46






                    • 4





                      Not great: 1. doesn't work inplace, 2. can replace r also not at EOL (which may or may not be what you want...).

                      – Tomasz Gandor
                      Jul 9 '14 at 10:33






                    • 4





                      1. Most unixy tools work that way, and it's usually the safest way to go about things since if you screw up you still have the original. 2. The question as stated is to remove carriage returns, not to convert line endings. But there are plenty of other answers that might serve you better.

                      – Henrik Gustafsson
                      Jul 9 '14 at 11:56






                    • 1





                      If your tr does not support the r escape, try '15' or perhaps a literal '^M' (in many shells on many terminals, ctrl-V ctrl-M will produce a literal ctrl-M character).

                      – tripleee
                      Aug 25 '14 at 10:55








                    • 1





                      This is what works on AIX.

                      – Jesse Chisholm
                      Dec 30 '15 at 21:09














                    205












                    205








                    205







                    tr -d 'r' < infile > outfile


                    See tr(1)






                    share|improve this answer













                    tr -d 'r' < infile > outfile


                    See tr(1)







                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Apr 29 '09 at 13:48









                    Henrik GustafssonHenrik Gustafsson

                    32.3k74060




                    32.3k74060








                    • 1





                      Very simple and reliable. thank you.

                      – maček
                      Jul 8 '14 at 21:46






                    • 4





                      Not great: 1. doesn't work inplace, 2. can replace r also not at EOL (which may or may not be what you want...).

                      – Tomasz Gandor
                      Jul 9 '14 at 10:33






                    • 4





                      1. Most unixy tools work that way, and it's usually the safest way to go about things since if you screw up you still have the original. 2. The question as stated is to remove carriage returns, not to convert line endings. But there are plenty of other answers that might serve you better.

                      – Henrik Gustafsson
                      Jul 9 '14 at 11:56






                    • 1





                      If your tr does not support the r escape, try '15' or perhaps a literal '^M' (in many shells on many terminals, ctrl-V ctrl-M will produce a literal ctrl-M character).

                      – tripleee
                      Aug 25 '14 at 10:55








                    • 1





                      This is what works on AIX.

                      – Jesse Chisholm
                      Dec 30 '15 at 21:09














                    • 1





                      Very simple and reliable. thank you.

                      – maček
                      Jul 8 '14 at 21:46






                    • 4





                      Not great: 1. doesn't work inplace, 2. can replace r also not at EOL (which may or may not be what you want...).

                      – Tomasz Gandor
                      Jul 9 '14 at 10:33






                    • 4





                      1. Most unixy tools work that way, and it's usually the safest way to go about things since if you screw up you still have the original. 2. The question as stated is to remove carriage returns, not to convert line endings. But there are plenty of other answers that might serve you better.

                      – Henrik Gustafsson
                      Jul 9 '14 at 11:56






                    • 1





                      If your tr does not support the r escape, try '15' or perhaps a literal '^M' (in many shells on many terminals, ctrl-V ctrl-M will produce a literal ctrl-M character).

                      – tripleee
                      Aug 25 '14 at 10:55








                    • 1





                      This is what works on AIX.

                      – Jesse Chisholm
                      Dec 30 '15 at 21:09








                    1




                    1





                    Very simple and reliable. thank you.

                    – maček
                    Jul 8 '14 at 21:46





                    Very simple and reliable. thank you.

                    – maček
                    Jul 8 '14 at 21:46




                    4




                    4





                    Not great: 1. doesn't work inplace, 2. can replace r also not at EOL (which may or may not be what you want...).

                    – Tomasz Gandor
                    Jul 9 '14 at 10:33





                    Not great: 1. doesn't work inplace, 2. can replace r also not at EOL (which may or may not be what you want...).

                    – Tomasz Gandor
                    Jul 9 '14 at 10:33




                    4




                    4





                    1. Most unixy tools work that way, and it's usually the safest way to go about things since if you screw up you still have the original. 2. The question as stated is to remove carriage returns, not to convert line endings. But there are plenty of other answers that might serve you better.

                    – Henrik Gustafsson
                    Jul 9 '14 at 11:56





                    1. Most unixy tools work that way, and it's usually the safest way to go about things since if you screw up you still have the original. 2. The question as stated is to remove carriage returns, not to convert line endings. But there are plenty of other answers that might serve you better.

                    – Henrik Gustafsson
                    Jul 9 '14 at 11:56




                    1




                    1





                    If your tr does not support the r escape, try '15' or perhaps a literal '^M' (in many shells on many terminals, ctrl-V ctrl-M will produce a literal ctrl-M character).

                    – tripleee
                    Aug 25 '14 at 10:55







                    If your tr does not support the r escape, try '15' or perhaps a literal '^M' (in many shells on many terminals, ctrl-V ctrl-M will produce a literal ctrl-M character).

                    – tripleee
                    Aug 25 '14 at 10:55






                    1




                    1





                    This is what works on AIX.

                    – Jesse Chisholm
                    Dec 30 '15 at 21:09





                    This is what works on AIX.

                    – Jesse Chisholm
                    Dec 30 '15 at 21:09











                    34














                    Old School:



                    tr -d 'r' < filewithcarriagereturns > filewithoutcarriagereturns





                    share|improve this answer



















                    • 1





                      This worked like charm :) Thanks.

                      – user1336087
                      Oct 29 '14 at 15:36








                    • 3





                      Just don't use the same file as the destination...

                      – Hejazzman
                      Dec 29 '17 at 8:13
















                    34














                    Old School:



                    tr -d 'r' < filewithcarriagereturns > filewithoutcarriagereturns





                    share|improve this answer



















                    • 1





                      This worked like charm :) Thanks.

                      – user1336087
                      Oct 29 '14 at 15:36








                    • 3





                      Just don't use the same file as the destination...

                      – Hejazzman
                      Dec 29 '17 at 8:13














                    34












                    34








                    34







                    Old School:



                    tr -d 'r' < filewithcarriagereturns > filewithoutcarriagereturns





                    share|improve this answer













                    Old School:



                    tr -d 'r' < filewithcarriagereturns > filewithoutcarriagereturns






                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Apr 29 '09 at 13:50









                    plinthplinth

                    40.8k868111




                    40.8k868111








                    • 1





                      This worked like charm :) Thanks.

                      – user1336087
                      Oct 29 '14 at 15:36








                    • 3





                      Just don't use the same file as the destination...

                      – Hejazzman
                      Dec 29 '17 at 8:13














                    • 1





                      This worked like charm :) Thanks.

                      – user1336087
                      Oct 29 '14 at 15:36








                    • 3





                      Just don't use the same file as the destination...

                      – Hejazzman
                      Dec 29 '17 at 8:13








                    1




                    1





                    This worked like charm :) Thanks.

                    – user1336087
                    Oct 29 '14 at 15:36







                    This worked like charm :) Thanks.

                    – user1336087
                    Oct 29 '14 at 15:36






                    3




                    3





                    Just don't use the same file as the destination...

                    – Hejazzman
                    Dec 29 '17 at 8:13





                    Just don't use the same file as the destination...

                    – Hejazzman
                    Dec 29 '17 at 8:13











                    27














                    There's a utility called dos2unix that exists on many systems, and can be easily installed on most.






                    share|improve this answer



















                    • 6





                      Sometimes it is also called fromdos (and todos).

                      – Anonymous
                      Apr 29 '09 at 13:59
















                    27














                    There's a utility called dos2unix that exists on many systems, and can be easily installed on most.






                    share|improve this answer



















                    • 6





                      Sometimes it is also called fromdos (and todos).

                      – Anonymous
                      Apr 29 '09 at 13:59














                    27












                    27








                    27







                    There's a utility called dos2unix that exists on many systems, and can be easily installed on most.






                    share|improve this answer













                    There's a utility called dos2unix that exists on many systems, and can be easily installed on most.







                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Apr 28 '09 at 22:10









                    Emil HEmil H

                    34.3k106791




                    34.3k106791








                    • 6





                      Sometimes it is also called fromdos (and todos).

                      – Anonymous
                      Apr 29 '09 at 13:59














                    • 6





                      Sometimes it is also called fromdos (and todos).

                      – Anonymous
                      Apr 29 '09 at 13:59








                    6




                    6





                    Sometimes it is also called fromdos (and todos).

                    – Anonymous
                    Apr 29 '09 at 13:59





                    Sometimes it is also called fromdos (and todos).

                    – Anonymous
                    Apr 29 '09 at 13:59











                    17














                    The simplest way on Linux is, in my humble opinion,



                    sed -i 's/r//g' <filename>


                    The strong quotes around the substitution operator 's/r//' are essential. Without them the shell will interpret r as an escape+r and reduce it to a plain r, and remove all lower case r. That's why the answer given above in 2009 by Rob doesn't work.



                    And adding the /g modifier ensures that even multiple r will be removed, and not only the first one.






                    share|improve this answer






























                      17














                      The simplest way on Linux is, in my humble opinion,



                      sed -i 's/r//g' <filename>


                      The strong quotes around the substitution operator 's/r//' are essential. Without them the shell will interpret r as an escape+r and reduce it to a plain r, and remove all lower case r. That's why the answer given above in 2009 by Rob doesn't work.



                      And adding the /g modifier ensures that even multiple r will be removed, and not only the first one.






                      share|improve this answer




























                        17












                        17








                        17







                        The simplest way on Linux is, in my humble opinion,



                        sed -i 's/r//g' <filename>


                        The strong quotes around the substitution operator 's/r//' are essential. Without them the shell will interpret r as an escape+r and reduce it to a plain r, and remove all lower case r. That's why the answer given above in 2009 by Rob doesn't work.



                        And adding the /g modifier ensures that even multiple r will be removed, and not only the first one.






                        share|improve this answer















                        The simplest way on Linux is, in my humble opinion,



                        sed -i 's/r//g' <filename>


                        The strong quotes around the substitution operator 's/r//' are essential. Without them the shell will interpret r as an escape+r and reduce it to a plain r, and remove all lower case r. That's why the answer given above in 2009 by Rob doesn't work.



                        And adding the /g modifier ensures that even multiple r will be removed, and not only the first one.







                        share|improve this answer














                        share|improve this answer



                        share|improve this answer








                        edited Nov 23 '18 at 19:10









                        Benjamin W.

                        20.6k134656




                        20.6k134656










                        answered Jan 4 '17 at 10:47









                        wfjmwfjm

                        33339




                        33339























                            7














                            sed -i s/r// <filename> or somesuch; see man sed or the wealth of information available on the web regarding use of sed.



                            One thing to point out is the precise meaning of "carriage return" in the above; if you truly mean the single control character "carriage return", then the pattern above is correct. If you meant, more generally, CRLF (carriage return and a line feed, which is how line feeds are implemented under Windows), then you probably want to replace rn instead. Bare line feeds (newline) in Linux/Unix are n.






                            share|improve this answer
























                            • I am trying to use --> sed 's/rn/=/' countryNew.txt > demo.txt which does not work. "tiger" "Lion."

                              – Suvasis
                              Sep 13 '13 at 7:12













                            • are we to take that to mean you're on a mac? I've noticed Darwin sed seems to have different commands and feature sets by default than most Linux versions...

                              – jsh
                              Jan 23 '14 at 17:51






                            • 4





                              FYI, the s/r// doesn't seem to remove carriage returns on OS X, it seems to remove literal r chars instead. I'm not sure why that is yet. Maybe it has something to do with the way the string is quoted? As a workaround, using CTRL-V + CTRL-M in place of r seems to work.

                              – user456814
                              May 15 '14 at 21:38
















                            7














                            sed -i s/r// <filename> or somesuch; see man sed or the wealth of information available on the web regarding use of sed.



                            One thing to point out is the precise meaning of "carriage return" in the above; if you truly mean the single control character "carriage return", then the pattern above is correct. If you meant, more generally, CRLF (carriage return and a line feed, which is how line feeds are implemented under Windows), then you probably want to replace rn instead. Bare line feeds (newline) in Linux/Unix are n.






                            share|improve this answer
























                            • I am trying to use --> sed 's/rn/=/' countryNew.txt > demo.txt which does not work. "tiger" "Lion."

                              – Suvasis
                              Sep 13 '13 at 7:12













                            • are we to take that to mean you're on a mac? I've noticed Darwin sed seems to have different commands and feature sets by default than most Linux versions...

                              – jsh
                              Jan 23 '14 at 17:51






                            • 4





                              FYI, the s/r// doesn't seem to remove carriage returns on OS X, it seems to remove literal r chars instead. I'm not sure why that is yet. Maybe it has something to do with the way the string is quoted? As a workaround, using CTRL-V + CTRL-M in place of r seems to work.

                              – user456814
                              May 15 '14 at 21:38














                            7












                            7








                            7







                            sed -i s/r// <filename> or somesuch; see man sed or the wealth of information available on the web regarding use of sed.



                            One thing to point out is the precise meaning of "carriage return" in the above; if you truly mean the single control character "carriage return", then the pattern above is correct. If you meant, more generally, CRLF (carriage return and a line feed, which is how line feeds are implemented under Windows), then you probably want to replace rn instead. Bare line feeds (newline) in Linux/Unix are n.






                            share|improve this answer













                            sed -i s/r// <filename> or somesuch; see man sed or the wealth of information available on the web regarding use of sed.



                            One thing to point out is the precise meaning of "carriage return" in the above; if you truly mean the single control character "carriage return", then the pattern above is correct. If you meant, more generally, CRLF (carriage return and a line feed, which is how line feeds are implemented under Windows), then you probably want to replace rn instead. Bare line feeds (newline) in Linux/Unix are n.







                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered Apr 28 '09 at 22:12









                            RobRob

                            43.7k36587




                            43.7k36587













                            • I am trying to use --> sed 's/rn/=/' countryNew.txt > demo.txt which does not work. "tiger" "Lion."

                              – Suvasis
                              Sep 13 '13 at 7:12













                            • are we to take that to mean you're on a mac? I've noticed Darwin sed seems to have different commands and feature sets by default than most Linux versions...

                              – jsh
                              Jan 23 '14 at 17:51






                            • 4





                              FYI, the s/r// doesn't seem to remove carriage returns on OS X, it seems to remove literal r chars instead. I'm not sure why that is yet. Maybe it has something to do with the way the string is quoted? As a workaround, using CTRL-V + CTRL-M in place of r seems to work.

                              – user456814
                              May 15 '14 at 21:38



















                            • I am trying to use --> sed 's/rn/=/' countryNew.txt > demo.txt which does not work. "tiger" "Lion."

                              – Suvasis
                              Sep 13 '13 at 7:12













                            • are we to take that to mean you're on a mac? I've noticed Darwin sed seems to have different commands and feature sets by default than most Linux versions...

                              – jsh
                              Jan 23 '14 at 17:51






                            • 4





                              FYI, the s/r// doesn't seem to remove carriage returns on OS X, it seems to remove literal r chars instead. I'm not sure why that is yet. Maybe it has something to do with the way the string is quoted? As a workaround, using CTRL-V + CTRL-M in place of r seems to work.

                              – user456814
                              May 15 '14 at 21:38

















                            I am trying to use --> sed 's/rn/=/' countryNew.txt > demo.txt which does not work. "tiger" "Lion."

                            – Suvasis
                            Sep 13 '13 at 7:12







                            I am trying to use --> sed 's/rn/=/' countryNew.txt > demo.txt which does not work. "tiger" "Lion."

                            – Suvasis
                            Sep 13 '13 at 7:12















                            are we to take that to mean you're on a mac? I've noticed Darwin sed seems to have different commands and feature sets by default than most Linux versions...

                            – jsh
                            Jan 23 '14 at 17:51





                            are we to take that to mean you're on a mac? I've noticed Darwin sed seems to have different commands and feature sets by default than most Linux versions...

                            – jsh
                            Jan 23 '14 at 17:51




                            4




                            4





                            FYI, the s/r// doesn't seem to remove carriage returns on OS X, it seems to remove literal r chars instead. I'm not sure why that is yet. Maybe it has something to do with the way the string is quoted? As a workaround, using CTRL-V + CTRL-M in place of r seems to work.

                            – user456814
                            May 15 '14 at 21:38





                            FYI, the s/r// doesn't seem to remove carriage returns on OS X, it seems to remove literal r chars instead. I'm not sure why that is yet. Maybe it has something to do with the way the string is quoted? As a workaround, using CTRL-V + CTRL-M in place of r seems to work.

                            – user456814
                            May 15 '14 at 21:38











                            6














                            If you are a Vi user, you may open the file and remove the carriage return with:



                            :%s/r//g


                            or with



                            :1,$ s/^M//


                            Note that you should type ^M by pressing ctrl-v and then ctrl-m.






                            share|improve this answer



















                            • 2





                              Not great: if the file has CR on every line (i.e. is a correct DOS file), vim will load it with filetype=dos, and not show ^M-s at all. Getting around this is a ton of keystrokes, which is not what vim is made for ;). I'd just go for sed -i, and then `-e 's/r$//g' to limit the removal to CRs at EOL.

                              – Tomasz Gandor
                              Jul 9 '14 at 10:35
















                            6














                            If you are a Vi user, you may open the file and remove the carriage return with:



                            :%s/r//g


                            or with



                            :1,$ s/^M//


                            Note that you should type ^M by pressing ctrl-v and then ctrl-m.






                            share|improve this answer



















                            • 2





                              Not great: if the file has CR on every line (i.e. is a correct DOS file), vim will load it with filetype=dos, and not show ^M-s at all. Getting around this is a ton of keystrokes, which is not what vim is made for ;). I'd just go for sed -i, and then `-e 's/r$//g' to limit the removal to CRs at EOL.

                              – Tomasz Gandor
                              Jul 9 '14 at 10:35














                            6












                            6








                            6







                            If you are a Vi user, you may open the file and remove the carriage return with:



                            :%s/r//g


                            or with



                            :1,$ s/^M//


                            Note that you should type ^M by pressing ctrl-v and then ctrl-m.






                            share|improve this answer













                            If you are a Vi user, you may open the file and remove the carriage return with:



                            :%s/r//g


                            or with



                            :1,$ s/^M//


                            Note that you should type ^M by pressing ctrl-v and then ctrl-m.







                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered Sep 5 '12 at 11:13









                            Alex GiotisAlex Giotis

                            556611




                            556611








                            • 2





                              Not great: if the file has CR on every line (i.e. is a correct DOS file), vim will load it with filetype=dos, and not show ^M-s at all. Getting around this is a ton of keystrokes, which is not what vim is made for ;). I'd just go for sed -i, and then `-e 's/r$//g' to limit the removal to CRs at EOL.

                              – Tomasz Gandor
                              Jul 9 '14 at 10:35














                            • 2





                              Not great: if the file has CR on every line (i.e. is a correct DOS file), vim will load it with filetype=dos, and not show ^M-s at all. Getting around this is a ton of keystrokes, which is not what vim is made for ;). I'd just go for sed -i, and then `-e 's/r$//g' to limit the removal to CRs at EOL.

                              – Tomasz Gandor
                              Jul 9 '14 at 10:35








                            2




                            2





                            Not great: if the file has CR on every line (i.e. is a correct DOS file), vim will load it with filetype=dos, and not show ^M-s at all. Getting around this is a ton of keystrokes, which is not what vim is made for ;). I'd just go for sed -i, and then `-e 's/r$//g' to limit the removal to CRs at EOL.

                            – Tomasz Gandor
                            Jul 9 '14 at 10:35





                            Not great: if the file has CR on every line (i.e. is a correct DOS file), vim will load it with filetype=dos, and not show ^M-s at all. Getting around this is a ton of keystrokes, which is not what vim is made for ;). I'd just go for sed -i, and then `-e 's/r$//g' to limit the removal to CRs at EOL.

                            – Tomasz Gandor
                            Jul 9 '14 at 10:35











                            6














                            Once more a solution... Because there's always one more:



                            perl -i -pe 's/r//' filename


                            It's nice because it's in place and works in every flavor of unix/linux I've worked with.






                            share|improve this answer






























                              6














                              Once more a solution... Because there's always one more:



                              perl -i -pe 's/r//' filename


                              It's nice because it's in place and works in every flavor of unix/linux I've worked with.






                              share|improve this answer




























                                6












                                6








                                6







                                Once more a solution... Because there's always one more:



                                perl -i -pe 's/r//' filename


                                It's nice because it's in place and works in every flavor of unix/linux I've worked with.






                                share|improve this answer















                                Once more a solution... Because there's always one more:



                                perl -i -pe 's/r//' filename


                                It's nice because it's in place and works in every flavor of unix/linux I've worked with.







                                share|improve this answer














                                share|improve this answer



                                share|improve this answer








                                edited Jan 28 '16 at 16:08









                                Chris G

                                725619




                                725619










                                answered Jan 28 '16 at 15:09









                                Allan CanoAllan Cano

                                6111




                                6111























                                    3














                                    Someone else recommend dos2unix and I strongly recommend it as well. I'm just providing more details.



                                    If installed, jump to the next step. If not already installed, I would recommend installing it via yum like:



                                    yum install dos2unix


                                    Then you can use it like:



                                    dos2unix fileIWantToRemoveWindowsReturnsFrom.txt





                                    share|improve this answer




























                                      3














                                      Someone else recommend dos2unix and I strongly recommend it as well. I'm just providing more details.



                                      If installed, jump to the next step. If not already installed, I would recommend installing it via yum like:



                                      yum install dos2unix


                                      Then you can use it like:



                                      dos2unix fileIWantToRemoveWindowsReturnsFrom.txt





                                      share|improve this answer


























                                        3












                                        3








                                        3







                                        Someone else recommend dos2unix and I strongly recommend it as well. I'm just providing more details.



                                        If installed, jump to the next step. If not already installed, I would recommend installing it via yum like:



                                        yum install dos2unix


                                        Then you can use it like:



                                        dos2unix fileIWantToRemoveWindowsReturnsFrom.txt





                                        share|improve this answer













                                        Someone else recommend dos2unix and I strongly recommend it as well. I'm just providing more details.



                                        If installed, jump to the next step. If not already installed, I would recommend installing it via yum like:



                                        yum install dos2unix


                                        Then you can use it like:



                                        dos2unix fileIWantToRemoveWindowsReturnsFrom.txt






                                        share|improve this answer












                                        share|improve this answer



                                        share|improve this answer










                                        answered Jul 23 '15 at 15:25









                                        James OravecJames Oravec

                                        10k1961117




                                        10k1961117























                                            2














                                            Here is the thing,



                                            %0d is the carriage return character. To make it compatabile with Unix. We need to use the below command.



                                            dos2unix fileName.extension fileName.extension






                                            share|improve this answer




























                                              2














                                              Here is the thing,



                                              %0d is the carriage return character. To make it compatabile with Unix. We need to use the below command.



                                              dos2unix fileName.extension fileName.extension






                                              share|improve this answer


























                                                2












                                                2








                                                2







                                                Here is the thing,



                                                %0d is the carriage return character. To make it compatabile with Unix. We need to use the below command.



                                                dos2unix fileName.extension fileName.extension






                                                share|improve this answer













                                                Here is the thing,



                                                %0d is the carriage return character. To make it compatabile with Unix. We need to use the below command.



                                                dos2unix fileName.extension fileName.extension







                                                share|improve this answer












                                                share|improve this answer



                                                share|improve this answer










                                                answered Dec 17 '15 at 20:18









                                                Sireesh YarlagaddaSireesh Yarlagadda

                                                7,37824762




                                                7,37824762























                                                    1














                                                    try this to convert dos file into unix file:




                                                    fromdos file







                                                    share|improve this answer




























                                                      1














                                                      try this to convert dos file into unix file:




                                                      fromdos file







                                                      share|improve this answer


























                                                        1












                                                        1








                                                        1







                                                        try this to convert dos file into unix file:




                                                        fromdos file







                                                        share|improve this answer













                                                        try this to convert dos file into unix file:




                                                        fromdos file








                                                        share|improve this answer












                                                        share|improve this answer



                                                        share|improve this answer










                                                        answered Jul 20 '10 at 9:50









                                                        hawstonhawston

                                                        211




                                                        211























                                                            1














                                                            If you're using an OS (like OS X) that doesn't have the dos2unix command but does have a Python interpreter (version 2.5+), this command is equivalent to the dos2unix command:



                                                            python -c "import sys; import fileinput; sys.stdout.writelines(line.replace('r', 'n') for line in fileinput.input(mode='rU'))"


                                                            This handles both named files on the command line as well as pipes and redirects, just like dos2unix. If you add this line to your ~/.bashrc file (or equivalent profile file for other shells):



                                                            alias dos2unix="python -c "import sys; import fileinput; sys.stdout.writelines(line.replace('r', 'n') for line in fileinput.input(mode='rU'))""


                                                            ... the next time you log in (or run source ~/.bashrc in the current session) you will be able to use the dos2unix name on the command line in the same manner as in the other examples.






                                                            share|improve this answer






























                                                              1














                                                              If you're using an OS (like OS X) that doesn't have the dos2unix command but does have a Python interpreter (version 2.5+), this command is equivalent to the dos2unix command:



                                                              python -c "import sys; import fileinput; sys.stdout.writelines(line.replace('r', 'n') for line in fileinput.input(mode='rU'))"


                                                              This handles both named files on the command line as well as pipes and redirects, just like dos2unix. If you add this line to your ~/.bashrc file (or equivalent profile file for other shells):



                                                              alias dos2unix="python -c "import sys; import fileinput; sys.stdout.writelines(line.replace('r', 'n') for line in fileinput.input(mode='rU'))""


                                                              ... the next time you log in (or run source ~/.bashrc in the current session) you will be able to use the dos2unix name on the command line in the same manner as in the other examples.






                                                              share|improve this answer




























                                                                1












                                                                1








                                                                1







                                                                If you're using an OS (like OS X) that doesn't have the dos2unix command but does have a Python interpreter (version 2.5+), this command is equivalent to the dos2unix command:



                                                                python -c "import sys; import fileinput; sys.stdout.writelines(line.replace('r', 'n') for line in fileinput.input(mode='rU'))"


                                                                This handles both named files on the command line as well as pipes and redirects, just like dos2unix. If you add this line to your ~/.bashrc file (or equivalent profile file for other shells):



                                                                alias dos2unix="python -c "import sys; import fileinput; sys.stdout.writelines(line.replace('r', 'n') for line in fileinput.input(mode='rU'))""


                                                                ... the next time you log in (or run source ~/.bashrc in the current session) you will be able to use the dos2unix name on the command line in the same manner as in the other examples.






                                                                share|improve this answer















                                                                If you're using an OS (like OS X) that doesn't have the dos2unix command but does have a Python interpreter (version 2.5+), this command is equivalent to the dos2unix command:



                                                                python -c "import sys; import fileinput; sys.stdout.writelines(line.replace('r', 'n') for line in fileinput.input(mode='rU'))"


                                                                This handles both named files on the command line as well as pipes and redirects, just like dos2unix. If you add this line to your ~/.bashrc file (or equivalent profile file for other shells):



                                                                alias dos2unix="python -c "import sys; import fileinput; sys.stdout.writelines(line.replace('r', 'n') for line in fileinput.input(mode='rU'))""


                                                                ... the next time you log in (or run source ~/.bashrc in the current session) you will be able to use the dos2unix name on the command line in the same manner as in the other examples.







                                                                share|improve this answer














                                                                share|improve this answer



                                                                share|improve this answer








                                                                edited Apr 13 '15 at 15:27

























                                                                answered Nov 28 '12 at 18:16









                                                                Chris JohnsonChris Johnson

                                                                12.2k35359




                                                                12.2k35359























                                                                    1














                                                                    For UNIX... I've noticed dos2unix removed Unicode headers form my UTF-8 file. Under git bash (Windows), the following script seems to work nicely. It uses sed. Note it only removes carriage-returns at the ends of lines, and preserves Unicode headers.



                                                                    #!/bin/bash

                                                                    inOutFile="$1"
                                                                    backupFile="${inOutFile}~"
                                                                    mv --verbose "$inOutFile" "$backupFile"
                                                                    sed -e 's/15$//g' <"$backupFile" >"$inOutFile"





                                                                    share|improve this answer




























                                                                      1














                                                                      For UNIX... I've noticed dos2unix removed Unicode headers form my UTF-8 file. Under git bash (Windows), the following script seems to work nicely. It uses sed. Note it only removes carriage-returns at the ends of lines, and preserves Unicode headers.



                                                                      #!/bin/bash

                                                                      inOutFile="$1"
                                                                      backupFile="${inOutFile}~"
                                                                      mv --verbose "$inOutFile" "$backupFile"
                                                                      sed -e 's/15$//g' <"$backupFile" >"$inOutFile"





                                                                      share|improve this answer


























                                                                        1












                                                                        1








                                                                        1







                                                                        For UNIX... I've noticed dos2unix removed Unicode headers form my UTF-8 file. Under git bash (Windows), the following script seems to work nicely. It uses sed. Note it only removes carriage-returns at the ends of lines, and preserves Unicode headers.



                                                                        #!/bin/bash

                                                                        inOutFile="$1"
                                                                        backupFile="${inOutFile}~"
                                                                        mv --verbose "$inOutFile" "$backupFile"
                                                                        sed -e 's/15$//g' <"$backupFile" >"$inOutFile"





                                                                        share|improve this answer













                                                                        For UNIX... I've noticed dos2unix removed Unicode headers form my UTF-8 file. Under git bash (Windows), the following script seems to work nicely. It uses sed. Note it only removes carriage-returns at the ends of lines, and preserves Unicode headers.



                                                                        #!/bin/bash

                                                                        inOutFile="$1"
                                                                        backupFile="${inOutFile}~"
                                                                        mv --verbose "$inOutFile" "$backupFile"
                                                                        sed -e 's/15$//g' <"$backupFile" >"$inOutFile"






                                                                        share|improve this answer












                                                                        share|improve this answer



                                                                        share|improve this answer










                                                                        answered Jun 29 '17 at 20:24









                                                                        LexieHankinsLexieHankins

                                                                        31619




                                                                        31619























                                                                            1














                                                                            If you are running an X environment and have a proper editor (visual studio code), then I would follow the reccomendation:



                                                                            Visual Studio Code: How to show line endings



                                                                            Just go to the bottom right corner of your screen, visual studio code will show you both the file encoding and the end of line convention followed by the file, an just with a simple click you can switch that around.



                                                                            Just use visual code as your replacement for notepad++ on a linux environment and you are set to go.






                                                                            share|improve this answer




























                                                                              1














                                                                              If you are running an X environment and have a proper editor (visual studio code), then I would follow the reccomendation:



                                                                              Visual Studio Code: How to show line endings



                                                                              Just go to the bottom right corner of your screen, visual studio code will show you both the file encoding and the end of line convention followed by the file, an just with a simple click you can switch that around.



                                                                              Just use visual code as your replacement for notepad++ on a linux environment and you are set to go.






                                                                              share|improve this answer


























                                                                                1












                                                                                1








                                                                                1







                                                                                If you are running an X environment and have a proper editor (visual studio code), then I would follow the reccomendation:



                                                                                Visual Studio Code: How to show line endings



                                                                                Just go to the bottom right corner of your screen, visual studio code will show you both the file encoding and the end of line convention followed by the file, an just with a simple click you can switch that around.



                                                                                Just use visual code as your replacement for notepad++ on a linux environment and you are set to go.






                                                                                share|improve this answer













                                                                                If you are running an X environment and have a proper editor (visual studio code), then I would follow the reccomendation:



                                                                                Visual Studio Code: How to show line endings



                                                                                Just go to the bottom right corner of your screen, visual studio code will show you both the file encoding and the end of line convention followed by the file, an just with a simple click you can switch that around.



                                                                                Just use visual code as your replacement for notepad++ on a linux environment and you are set to go.







                                                                                share|improve this answer












                                                                                share|improve this answer



                                                                                share|improve this answer










                                                                                answered Aug 18 '17 at 9:00









                                                                                99Sono99Sono

                                                                                2,1021830




                                                                                2,1021830























                                                                                    0














                                                                                    I've used python for it, here my code;



                                                                                    end1='/home/.../file1.txt'
                                                                                    end2='/home/.../file2.txt'
                                                                                    with open(end1, "rb") as inf:
                                                                                    with open(end2, "w") as fixed:
                                                                                    for line in inf:
                                                                                    line = line.replace("n", "")
                                                                                    line = line.replace("r", "")
                                                                                    fixed.write(line)





                                                                                    share|improve this answer




























                                                                                      0














                                                                                      I've used python for it, here my code;



                                                                                      end1='/home/.../file1.txt'
                                                                                      end2='/home/.../file2.txt'
                                                                                      with open(end1, "rb") as inf:
                                                                                      with open(end2, "w") as fixed:
                                                                                      for line in inf:
                                                                                      line = line.replace("n", "")
                                                                                      line = line.replace("r", "")
                                                                                      fixed.write(line)





                                                                                      share|improve this answer


























                                                                                        0












                                                                                        0








                                                                                        0







                                                                                        I've used python for it, here my code;



                                                                                        end1='/home/.../file1.txt'
                                                                                        end2='/home/.../file2.txt'
                                                                                        with open(end1, "rb") as inf:
                                                                                        with open(end2, "w") as fixed:
                                                                                        for line in inf:
                                                                                        line = line.replace("n", "")
                                                                                        line = line.replace("r", "")
                                                                                        fixed.write(line)





                                                                                        share|improve this answer













                                                                                        I've used python for it, here my code;



                                                                                        end1='/home/.../file1.txt'
                                                                                        end2='/home/.../file2.txt'
                                                                                        with open(end1, "rb") as inf:
                                                                                        with open(end2, "w") as fixed:
                                                                                        for line in inf:
                                                                                        line = line.replace("n", "")
                                                                                        line = line.replace("r", "")
                                                                                        fixed.write(line)






                                                                                        share|improve this answer












                                                                                        share|improve this answer



                                                                                        share|improve this answer










                                                                                        answered Mar 10 '16 at 1:15









                                                                                        RaphaelRaphael

                                                                                        549




                                                                                        549























                                                                                            -1














                                                                                            you can simply do this :



                                                                                            $ echo $(cat input) > output





                                                                                            share|improve this answer
























                                                                                            • Don't know why someone gave '-1'. This is a perfectly good answer (and the only one which worked for me).

                                                                                              – FractalSpace
                                                                                              Jun 22 '15 at 16:43






                                                                                            • 1





                                                                                              Oh, sorry, it was me. Wait, look, it really does not work for 'r'!

                                                                                              – Viacheslav Rodionov
                                                                                              Jun 25 '15 at 13:21






                                                                                            • 1





                                                                                              @FractalSpace This is a terrible idea! It completely wrecks all the spacing in the file and leaves all the contents of the file subject to interpretation by the shell. Try it with a file that contains one line a * b...

                                                                                              – Tom Fenech
                                                                                              Jan 28 '16 at 10:37
















                                                                                            -1














                                                                                            you can simply do this :



                                                                                            $ echo $(cat input) > output





                                                                                            share|improve this answer
























                                                                                            • Don't know why someone gave '-1'. This is a perfectly good answer (and the only one which worked for me).

                                                                                              – FractalSpace
                                                                                              Jun 22 '15 at 16:43






                                                                                            • 1





                                                                                              Oh, sorry, it was me. Wait, look, it really does not work for 'r'!

                                                                                              – Viacheslav Rodionov
                                                                                              Jun 25 '15 at 13:21






                                                                                            • 1





                                                                                              @FractalSpace This is a terrible idea! It completely wrecks all the spacing in the file and leaves all the contents of the file subject to interpretation by the shell. Try it with a file that contains one line a * b...

                                                                                              – Tom Fenech
                                                                                              Jan 28 '16 at 10:37














                                                                                            -1












                                                                                            -1








                                                                                            -1







                                                                                            you can simply do this :



                                                                                            $ echo $(cat input) > output





                                                                                            share|improve this answer













                                                                                            you can simply do this :



                                                                                            $ echo $(cat input) > output






                                                                                            share|improve this answer












                                                                                            share|improve this answer



                                                                                            share|improve this answer










                                                                                            answered Apr 21 '15 at 12:58









                                                                                            mma7mma7

                                                                                            10617




                                                                                            10617













                                                                                            • Don't know why someone gave '-1'. This is a perfectly good answer (and the only one which worked for me).

                                                                                              – FractalSpace
                                                                                              Jun 22 '15 at 16:43






                                                                                            • 1





                                                                                              Oh, sorry, it was me. Wait, look, it really does not work for 'r'!

                                                                                              – Viacheslav Rodionov
                                                                                              Jun 25 '15 at 13:21






                                                                                            • 1





                                                                                              @FractalSpace This is a terrible idea! It completely wrecks all the spacing in the file and leaves all the contents of the file subject to interpretation by the shell. Try it with a file that contains one line a * b...

                                                                                              – Tom Fenech
                                                                                              Jan 28 '16 at 10:37



















                                                                                            • Don't know why someone gave '-1'. This is a perfectly good answer (and the only one which worked for me).

                                                                                              – FractalSpace
                                                                                              Jun 22 '15 at 16:43






                                                                                            • 1





                                                                                              Oh, sorry, it was me. Wait, look, it really does not work for 'r'!

                                                                                              – Viacheslav Rodionov
                                                                                              Jun 25 '15 at 13:21






                                                                                            • 1





                                                                                              @FractalSpace This is a terrible idea! It completely wrecks all the spacing in the file and leaves all the contents of the file subject to interpretation by the shell. Try it with a file that contains one line a * b...

                                                                                              – Tom Fenech
                                                                                              Jan 28 '16 at 10:37

















                                                                                            Don't know why someone gave '-1'. This is a perfectly good answer (and the only one which worked for me).

                                                                                            – FractalSpace
                                                                                            Jun 22 '15 at 16:43





                                                                                            Don't know why someone gave '-1'. This is a perfectly good answer (and the only one which worked for me).

                                                                                            – FractalSpace
                                                                                            Jun 22 '15 at 16:43




                                                                                            1




                                                                                            1





                                                                                            Oh, sorry, it was me. Wait, look, it really does not work for 'r'!

                                                                                            – Viacheslav Rodionov
                                                                                            Jun 25 '15 at 13:21





                                                                                            Oh, sorry, it was me. Wait, look, it really does not work for 'r'!

                                                                                            – Viacheslav Rodionov
                                                                                            Jun 25 '15 at 13:21




                                                                                            1




                                                                                            1





                                                                                            @FractalSpace This is a terrible idea! It completely wrecks all the spacing in the file and leaves all the contents of the file subject to interpretation by the shell. Try it with a file that contains one line a * b...

                                                                                            – Tom Fenech
                                                                                            Jan 28 '16 at 10:37





                                                                                            @FractalSpace This is a terrible idea! It completely wrecks all the spacing in the file and leaves all the contents of the file subject to interpretation by the shell. Try it with a file that contains one line a * b...

                                                                                            – Tom Fenech
                                                                                            Jan 28 '16 at 10:37


















                                                                                            draft saved

                                                                                            draft discarded




















































                                                                                            Thanks for contributing an answer to Stack Overflow!


                                                                                            • Please be sure to answer the question. Provide details and share your research!

                                                                                            But avoid



                                                                                            • Asking for help, clarification, or responding to other answers.

                                                                                            • Making statements based on opinion; back them up with references or personal experience.


                                                                                            To learn more, see our tips on writing great answers.




                                                                                            draft saved


                                                                                            draft discarded














                                                                                            StackExchange.ready(
                                                                                            function () {
                                                                                            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f800030%2fremove-carriage-return-in-unix%23new-answer', 'question_page');
                                                                                            }
                                                                                            );

                                                                                            Post as a guest















                                                                                            Required, but never shown





















































                                                                                            Required, but never shown














                                                                                            Required, but never shown












                                                                                            Required, but never shown







                                                                                            Required, but never shown

































                                                                                            Required, but never shown














                                                                                            Required, but never shown












                                                                                            Required, but never shown







                                                                                            Required, but never shown







                                                                                            Popular posts from this blog

                                                                                            Berounka

                                                                                            Sphinx de Gizeh

                                                                                            Different font size/position of beamer's navigation symbols template's content depending on regular/plain...