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

30.9k846105




30.9k846105









asked Aug 6 at 19:15









Dave

363626




363626











  • 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










3 Answers
3






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

















    up vote
    -1
    down vote













    You can do this without awk as well:



    IFS=','
    while read a b
    do
    echo $b, $a
    done <<!
    num_logins,day
    253,2016-07-01
    127,2016-07-02
    !


    BTW, I used what I think is called a 'here document' (?) - that is the construction with:



    ....<<!
    ...
    !


    This can be replaced with:



    ... < /tmp/2016_logins.csv





    share|improve this answer





















    • You should put those variables in quotes: echo "$b, $a".  printf "%sn" "$b, $a" or printf "%s, %sn" "$b" "$a" would be better.
      – G-Man
      Aug 7 at 9:28










    • Should? I don't think so: it works. There may be situations in general where it is necessary, but this is OK as it stands. The purpose wasn't to show a perfect script, only to illustrate a principle.
      – j4nd3r53n
      Aug 7 at 13:40











    • Your solution works for the sample data.  That’s a pretty low bar to clear; we prefer answers that work for any data consistent with the question.  Try your answer with an input of *,2018-08-08.
      – G-Man
      Aug 8 at 1:33











    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






























    3 Answers
    3






    active

    oldest

    votes








    3 Answers
    3






    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.1k22047




            12.1k22047







            • 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










            up vote
            -1
            down vote













            You can do this without awk as well:



            IFS=','
            while read a b
            do
            echo $b, $a
            done <<!
            num_logins,day
            253,2016-07-01
            127,2016-07-02
            !


            BTW, I used what I think is called a 'here document' (?) - that is the construction with:



            ....<<!
            ...
            !


            This can be replaced with:



            ... < /tmp/2016_logins.csv





            share|improve this answer





















            • You should put those variables in quotes: echo "$b, $a".  printf "%sn" "$b, $a" or printf "%s, %sn" "$b" "$a" would be better.
              – G-Man
              Aug 7 at 9:28










            • Should? I don't think so: it works. There may be situations in general where it is necessary, but this is OK as it stands. The purpose wasn't to show a perfect script, only to illustrate a principle.
              – j4nd3r53n
              Aug 7 at 13:40











            • Your solution works for the sample data.  That’s a pretty low bar to clear; we prefer answers that work for any data consistent with the question.  Try your answer with an input of *,2018-08-08.
              – G-Man
              Aug 8 at 1:33















            up vote
            -1
            down vote













            You can do this without awk as well:



            IFS=','
            while read a b
            do
            echo $b, $a
            done <<!
            num_logins,day
            253,2016-07-01
            127,2016-07-02
            !


            BTW, I used what I think is called a 'here document' (?) - that is the construction with:



            ....<<!
            ...
            !


            This can be replaced with:



            ... < /tmp/2016_logins.csv





            share|improve this answer





















            • You should put those variables in quotes: echo "$b, $a".  printf "%sn" "$b, $a" or printf "%s, %sn" "$b" "$a" would be better.
              – G-Man
              Aug 7 at 9:28










            • Should? I don't think so: it works. There may be situations in general where it is necessary, but this is OK as it stands. The purpose wasn't to show a perfect script, only to illustrate a principle.
              – j4nd3r53n
              Aug 7 at 13:40











            • Your solution works for the sample data.  That’s a pretty low bar to clear; we prefer answers that work for any data consistent with the question.  Try your answer with an input of *,2018-08-08.
              – G-Man
              Aug 8 at 1:33













            up vote
            -1
            down vote










            up vote
            -1
            down vote









            You can do this without awk as well:



            IFS=','
            while read a b
            do
            echo $b, $a
            done <<!
            num_logins,day
            253,2016-07-01
            127,2016-07-02
            !


            BTW, I used what I think is called a 'here document' (?) - that is the construction with:



            ....<<!
            ...
            !


            This can be replaced with:



            ... < /tmp/2016_logins.csv





            share|improve this answer













            You can do this without awk as well:



            IFS=','
            while read a b
            do
            echo $b, $a
            done <<!
            num_logins,day
            253,2016-07-01
            127,2016-07-02
            !


            BTW, I used what I think is called a 'here document' (?) - that is the construction with:



            ....<<!
            ...
            !


            This can be replaced with:



            ... < /tmp/2016_logins.csv






            share|improve this answer













            share|improve this answer



            share|improve this answer











            answered Aug 7 at 8:55









            j4nd3r53n

            99




            99











            • You should put those variables in quotes: echo "$b, $a".  printf "%sn" "$b, $a" or printf "%s, %sn" "$b" "$a" would be better.
              – G-Man
              Aug 7 at 9:28










            • Should? I don't think so: it works. There may be situations in general where it is necessary, but this is OK as it stands. The purpose wasn't to show a perfect script, only to illustrate a principle.
              – j4nd3r53n
              Aug 7 at 13:40











            • Your solution works for the sample data.  That’s a pretty low bar to clear; we prefer answers that work for any data consistent with the question.  Try your answer with an input of *,2018-08-08.
              – G-Man
              Aug 8 at 1:33

















            • You should put those variables in quotes: echo "$b, $a".  printf "%sn" "$b, $a" or printf "%s, %sn" "$b" "$a" would be better.
              – G-Man
              Aug 7 at 9:28










            • Should? I don't think so: it works. There may be situations in general where it is necessary, but this is OK as it stands. The purpose wasn't to show a perfect script, only to illustrate a principle.
              – j4nd3r53n
              Aug 7 at 13:40











            • Your solution works for the sample data.  That’s a pretty low bar to clear; we prefer answers that work for any data consistent with the question.  Try your answer with an input of *,2018-08-08.
              – G-Man
              Aug 8 at 1:33
















            You should put those variables in quotes: echo "$b, $a".  printf "%sn" "$b, $a" or printf "%s, %sn" "$b" "$a" would be better.
            – G-Man
            Aug 7 at 9:28




            You should put those variables in quotes: echo "$b, $a".  printf "%sn" "$b, $a" or printf "%s, %sn" "$b" "$a" would be better.
            – G-Man
            Aug 7 at 9:28












            Should? I don't think so: it works. There may be situations in general where it is necessary, but this is OK as it stands. The purpose wasn't to show a perfect script, only to illustrate a principle.
            – j4nd3r53n
            Aug 7 at 13:40





            Should? I don't think so: it works. There may be situations in general where it is necessary, but this is OK as it stands. The purpose wasn't to show a perfect script, only to illustrate a principle.
            – j4nd3r53n
            Aug 7 at 13:40













            Your solution works for the sample data.  That’s a pretty low bar to clear; we prefer answers that work for any data consistent with the question.  Try your answer with an input of *,2018-08-08.
            – G-Man
            Aug 8 at 1:33





            Your solution works for the sample data.  That’s a pretty low bar to clear; we prefer answers that work for any data consistent with the question.  Try your answer with an input of *,2018-08-08.
            – G-Man
            Aug 8 at 1:33













             

            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













































































            Popular posts from this blog

            Chat program with C++ and SFML

            Function to Return a JSON Like Objects Using VBA Collections and Arrays

            Will my employers contract hold up in court?