Using bash to swap first and second columns in CSV

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











up vote
1
down vote

favorite












I'm using bash. I have a CSV file with two columns of data that looks roughly like this



 num_logins,day
253,2016-07-01
127,2016-07-02


I want to swap the first and second columns (making the date column the first one). So I tried this



awk ' t = $1; $1 = $2; $2 = t; print; ' /tmp/2016_logins.csv 


However, the results are outputting the same . What am I missing in my awk statement above to get things to switch properly?







share|improve this question






















  • Bash isn’t a text editor...
    – Jeff Schaller
    Aug 6 at 20:21














up vote
1
down vote

favorite












I'm using bash. I have a CSV file with two columns of data that looks roughly like this



 num_logins,day
253,2016-07-01
127,2016-07-02


I want to swap the first and second columns (making the date column the first one). So I tried this



awk ' t = $1; $1 = $2; $2 = t; print; ' /tmp/2016_logins.csv 


However, the results are outputting the same . What am I missing in my awk statement above to get things to switch properly?







share|improve this question






















  • Bash isn’t a text editor...
    – Jeff Schaller
    Aug 6 at 20:21












up vote
1
down vote

favorite









up vote
1
down vote

favorite











I'm using bash. I have a CSV file with two columns of data that looks roughly like this



 num_logins,day
253,2016-07-01
127,2016-07-02


I want to swap the first and second columns (making the date column the first one). So I tried this



awk ' t = $1; $1 = $2; $2 = t; print; ' /tmp/2016_logins.csv 


However, the results are outputting the same . What am I missing in my awk statement above to get things to switch properly?







share|improve this question














I'm using bash. I have a CSV file with two columns of data that looks roughly like this



 num_logins,day
253,2016-07-01
127,2016-07-02


I want to swap the first and second columns (making the date column the first one). So I tried this



awk ' t = $1; $1 = $2; $2 = t; print; ' /tmp/2016_logins.csv 


However, the results are outputting the same . What am I missing in my awk statement above to get things to switch properly?









share|improve this question













share|improve this question




share|improve this question








edited Aug 6 at 20:22









Jeff Schaller

31.2k846105




31.2k846105










asked Aug 6 at 19:15









Dave

368827




368827











  • Bash isn’t a text editor...
    – Jeff Schaller
    Aug 6 at 20:21
















  • Bash isn’t a text editor...
    – Jeff Schaller
    Aug 6 at 20:21















Bash isn’t a text editor...
– Jeff Schaller
Aug 6 at 20:21




Bash isn’t a text editor...
– Jeff Schaller
Aug 6 at 20:21










2 Answers
2






active

oldest

votes

















up vote
3
down vote



accepted










Here you go :



 awk ' FS="," print $2 "," $1 ' sampleData.csv 





share|improve this answer



























    up vote
    5
    down vote













    Because default delimiter is space/tab. Not comma. Taking your original code, and adding a -F, solves it.



    $ awk -F, ' t = $1; $1 = $2; $2 = t; print; ' /tmp/2016_logins.csv
    day num_logins
    2016-07-01 253
    2016-07-02 127
    $


    Stripping it down to $0=$2" "$11 gets same result.



    $ awk -F, '$0=$2" "$11' /tmp/2016_logins.csv
    day num_logins
    2016-07-01 253
    2016-07-02 127
    $





    share|improve this answer


















    • 2




      Set OFS to get commas in the output too.
      – Kusalananda
      Aug 6 at 19:30










    • And this one would leave columns 3+ alone, rather than throwing them away.
      – Monty Harder
      Aug 6 at 22:47










    Your Answer







    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "106"
    ;
    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',
    convertImagesToLinks: false,
    noModals: false,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: null,
    bindNavPrevention: true,
    postfix: "",
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );








     

    draft saved


    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f460890%2fusing-bash-to-swap-first-and-second-columns-in-csv%23new-answer', 'question_page');

    );

    Post as a guest






























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    3
    down vote



    accepted










    Here you go :



     awk ' FS="," print $2 "," $1 ' sampleData.csv 





    share|improve this answer
























      up vote
      3
      down vote



      accepted










      Here you go :



       awk ' FS="," print $2 "," $1 ' sampleData.csv 





      share|improve this answer






















        up vote
        3
        down vote



        accepted







        up vote
        3
        down vote



        accepted






        Here you go :



         awk ' FS="," print $2 "," $1 ' sampleData.csv 





        share|improve this answer












        Here you go :



         awk ' FS="," print $2 "," $1 ' sampleData.csv 






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Aug 6 at 19:20









        Joe M

        5964




        5964






















            up vote
            5
            down vote













            Because default delimiter is space/tab. Not comma. Taking your original code, and adding a -F, solves it.



            $ awk -F, ' t = $1; $1 = $2; $2 = t; print; ' /tmp/2016_logins.csv
            day num_logins
            2016-07-01 253
            2016-07-02 127
            $


            Stripping it down to $0=$2" "$11 gets same result.



            $ awk -F, '$0=$2" "$11' /tmp/2016_logins.csv
            day num_logins
            2016-07-01 253
            2016-07-02 127
            $





            share|improve this answer


















            • 2




              Set OFS to get commas in the output too.
              – Kusalananda
              Aug 6 at 19:30










            • And this one would leave columns 3+ alone, rather than throwing them away.
              – Monty Harder
              Aug 6 at 22:47














            up vote
            5
            down vote













            Because default delimiter is space/tab. Not comma. Taking your original code, and adding a -F, solves it.



            $ awk -F, ' t = $1; $1 = $2; $2 = t; print; ' /tmp/2016_logins.csv
            day num_logins
            2016-07-01 253
            2016-07-02 127
            $


            Stripping it down to $0=$2" "$11 gets same result.



            $ awk -F, '$0=$2" "$11' /tmp/2016_logins.csv
            day num_logins
            2016-07-01 253
            2016-07-02 127
            $





            share|improve this answer


















            • 2




              Set OFS to get commas in the output too.
              – Kusalananda
              Aug 6 at 19:30










            • And this one would leave columns 3+ alone, rather than throwing them away.
              – Monty Harder
              Aug 6 at 22:47












            up vote
            5
            down vote










            up vote
            5
            down vote









            Because default delimiter is space/tab. Not comma. Taking your original code, and adding a -F, solves it.



            $ awk -F, ' t = $1; $1 = $2; $2 = t; print; ' /tmp/2016_logins.csv
            day num_logins
            2016-07-01 253
            2016-07-02 127
            $


            Stripping it down to $0=$2" "$11 gets same result.



            $ awk -F, '$0=$2" "$11' /tmp/2016_logins.csv
            day num_logins
            2016-07-01 253
            2016-07-02 127
            $





            share|improve this answer














            Because default delimiter is space/tab. Not comma. Taking your original code, and adding a -F, solves it.



            $ awk -F, ' t = $1; $1 = $2; $2 = t; print; ' /tmp/2016_logins.csv
            day num_logins
            2016-07-01 253
            2016-07-02 127
            $


            Stripping it down to $0=$2" "$11 gets same result.



            $ awk -F, '$0=$2" "$11' /tmp/2016_logins.csv
            day num_logins
            2016-07-01 253
            2016-07-02 127
            $






            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Aug 6 at 21:19

























            answered Aug 6 at 19:20









            steve

            12.3k22048




            12.3k22048







            • 2




              Set OFS to get commas in the output too.
              – Kusalananda
              Aug 6 at 19:30










            • And this one would leave columns 3+ alone, rather than throwing them away.
              – Monty Harder
              Aug 6 at 22:47












            • 2




              Set OFS to get commas in the output too.
              – Kusalananda
              Aug 6 at 19:30










            • And this one would leave columns 3+ alone, rather than throwing them away.
              – Monty Harder
              Aug 6 at 22:47







            2




            2




            Set OFS to get commas in the output too.
            – Kusalananda
            Aug 6 at 19:30




            Set OFS to get commas in the output too.
            – Kusalananda
            Aug 6 at 19:30












            And this one would leave columns 3+ alone, rather than throwing them away.
            – Monty Harder
            Aug 6 at 22:47




            And this one would leave columns 3+ alone, rather than throwing them away.
            – Monty Harder
            Aug 6 at 22:47












             

            draft saved


            draft discarded


























             


            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f460890%2fusing-bash-to-swap-first-and-second-columns-in-csv%23new-answer', 'question_page');

            );

            Post as a guest













































































            Comments

            Popular posts from this blog

            What does second last employer means? [closed]

            List of Gilmore Girls characters

            Confectionery