Swap lat lon and group by square brackets











up vote
1
down vote

favorite












I have A file which is having data like



input.csv
1;1.30453 103.83589;1.306663 103.83587;1.30 103.5;1.30693 103.83883;1.30709 103.835793;1.30725 103.835757;1.30741 103.83572;1.30753 103.8798
2;1.353 103.83589;1.306663 103.83587;1.30693 103.83583;1.30709 103.835793;1.30725 103.835757;1.30741 103.83572;1.30753 103.835
3;1.30653 103.83589;1.306663 103.83587;1.306797 103.83585;1.3693
.....


I am trying to convert this file to be in the format (I swap the lat and long values position and group them by a square bracket and seperate them by a comma)



output.csv

1,[103.83589,1.30453],[103.83587,1.306663],[103.5,1.30],[103.83883,1.30693],[103.835793,1.30709],[103.835757,1.30725],[103.83572,1.30741][103.8798,1.30753]


.... and so on.










share|improve this question




















  • 1




    Is 103.835871.30693 on line 2 of the input a misspelling? Also, there are seemingly random spaces added to the output, or is that also misspelling?
    – Ralph Rönnquist
    Mar 3 '17 at 5:49












  • Sorry for the Misspelling.Updated the question
    – RKR
    Mar 3 '17 at 5:56






  • 1




    mmm you probably meant to add a ; on the input rather than a space.(?) Is awk a requirement (given that this one is much easier with sed)
    – Ralph Rönnquist
    Mar 3 '17 at 6:00










  • No awk is not a requirement.sed is fine.
    – RKR
    Mar 3 '17 at 6:15















up vote
1
down vote

favorite












I have A file which is having data like



input.csv
1;1.30453 103.83589;1.306663 103.83587;1.30 103.5;1.30693 103.83883;1.30709 103.835793;1.30725 103.835757;1.30741 103.83572;1.30753 103.8798
2;1.353 103.83589;1.306663 103.83587;1.30693 103.83583;1.30709 103.835793;1.30725 103.835757;1.30741 103.83572;1.30753 103.835
3;1.30653 103.83589;1.306663 103.83587;1.306797 103.83585;1.3693
.....


I am trying to convert this file to be in the format (I swap the lat and long values position and group them by a square bracket and seperate them by a comma)



output.csv

1,[103.83589,1.30453],[103.83587,1.306663],[103.5,1.30],[103.83883,1.30693],[103.835793,1.30709],[103.835757,1.30725],[103.83572,1.30741][103.8798,1.30753]


.... and so on.










share|improve this question




















  • 1




    Is 103.835871.30693 on line 2 of the input a misspelling? Also, there are seemingly random spaces added to the output, or is that also misspelling?
    – Ralph Rönnquist
    Mar 3 '17 at 5:49












  • Sorry for the Misspelling.Updated the question
    – RKR
    Mar 3 '17 at 5:56






  • 1




    mmm you probably meant to add a ; on the input rather than a space.(?) Is awk a requirement (given that this one is much easier with sed)
    – Ralph Rönnquist
    Mar 3 '17 at 6:00










  • No awk is not a requirement.sed is fine.
    – RKR
    Mar 3 '17 at 6:15













up vote
1
down vote

favorite









up vote
1
down vote

favorite











I have A file which is having data like



input.csv
1;1.30453 103.83589;1.306663 103.83587;1.30 103.5;1.30693 103.83883;1.30709 103.835793;1.30725 103.835757;1.30741 103.83572;1.30753 103.8798
2;1.353 103.83589;1.306663 103.83587;1.30693 103.83583;1.30709 103.835793;1.30725 103.835757;1.30741 103.83572;1.30753 103.835
3;1.30653 103.83589;1.306663 103.83587;1.306797 103.83585;1.3693
.....


I am trying to convert this file to be in the format (I swap the lat and long values position and group them by a square bracket and seperate them by a comma)



output.csv

1,[103.83589,1.30453],[103.83587,1.306663],[103.5,1.30],[103.83883,1.30693],[103.835793,1.30709],[103.835757,1.30725],[103.83572,1.30741][103.8798,1.30753]


.... and so on.










share|improve this question















I have A file which is having data like



input.csv
1;1.30453 103.83589;1.306663 103.83587;1.30 103.5;1.30693 103.83883;1.30709 103.835793;1.30725 103.835757;1.30741 103.83572;1.30753 103.8798
2;1.353 103.83589;1.306663 103.83587;1.30693 103.83583;1.30709 103.835793;1.30725 103.835757;1.30741 103.83572;1.30753 103.835
3;1.30653 103.83589;1.306663 103.83587;1.306797 103.83585;1.3693
.....


I am trying to convert this file to be in the format (I swap the lat and long values position and group them by a square bracket and seperate them by a comma)



output.csv

1,[103.83589,1.30453],[103.83587,1.306663],[103.5,1.30],[103.83883,1.30693],[103.835793,1.30709],[103.835757,1.30725],[103.83572,1.30741][103.8798,1.30753]


.... and so on.







text-processing awk sed perl






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 25 at 14:59









Rui F Ribeiro

38.3k1475126




38.3k1475126










asked Mar 3 '17 at 4:22









RKR

23219




23219








  • 1




    Is 103.835871.30693 on line 2 of the input a misspelling? Also, there are seemingly random spaces added to the output, or is that also misspelling?
    – Ralph Rönnquist
    Mar 3 '17 at 5:49












  • Sorry for the Misspelling.Updated the question
    – RKR
    Mar 3 '17 at 5:56






  • 1




    mmm you probably meant to add a ; on the input rather than a space.(?) Is awk a requirement (given that this one is much easier with sed)
    – Ralph Rönnquist
    Mar 3 '17 at 6:00










  • No awk is not a requirement.sed is fine.
    – RKR
    Mar 3 '17 at 6:15














  • 1




    Is 103.835871.30693 on line 2 of the input a misspelling? Also, there are seemingly random spaces added to the output, or is that also misspelling?
    – Ralph Rönnquist
    Mar 3 '17 at 5:49












  • Sorry for the Misspelling.Updated the question
    – RKR
    Mar 3 '17 at 5:56






  • 1




    mmm you probably meant to add a ; on the input rather than a space.(?) Is awk a requirement (given that this one is much easier with sed)
    – Ralph Rönnquist
    Mar 3 '17 at 6:00










  • No awk is not a requirement.sed is fine.
    – RKR
    Mar 3 '17 at 6:15








1




1




Is 103.835871.30693 on line 2 of the input a misspelling? Also, there are seemingly random spaces added to the output, or is that also misspelling?
– Ralph Rönnquist
Mar 3 '17 at 5:49






Is 103.835871.30693 on line 2 of the input a misspelling? Also, there are seemingly random spaces added to the output, or is that also misspelling?
– Ralph Rönnquist
Mar 3 '17 at 5:49














Sorry for the Misspelling.Updated the question
– RKR
Mar 3 '17 at 5:56




Sorry for the Misspelling.Updated the question
– RKR
Mar 3 '17 at 5:56




1




1




mmm you probably meant to add a ; on the input rather than a space.(?) Is awk a requirement (given that this one is much easier with sed)
– Ralph Rönnquist
Mar 3 '17 at 6:00




mmm you probably meant to add a ; on the input rather than a space.(?) Is awk a requirement (given that this one is much easier with sed)
– Ralph Rönnquist
Mar 3 '17 at 6:00












No awk is not a requirement.sed is fine.
– RKR
Mar 3 '17 at 6:15




No awk is not a requirement.sed is fine.
– RKR
Mar 3 '17 at 6:15










3 Answers
3






active

oldest

votes

















up vote
2
down vote













sed 's/;([0-9.]*) ([0-9.]*)/,[2,1]/g' input.csv > output.csv


can do the job but in 2nd line there is 1.306663 103.83587 1.30693 103.83583 group which has no ; separator. If it is not a misprint script should be modified as



sed 's/[; ]?([0-9.]+) ([0-9.]+)/,[2,1]/g' input.csv > output.csv


which produce



1,[103.83589,1.30453],[103.83587,1.306663],[103.5,1.30],[103.83883,1.30693],[103.835793,1.30709],[103.835757,1.30725],[103.83572,1.30741],[103.8798,1.30753]
2,[103.83589,1.353],[103.83587,1.306663],[103.83583,1.30693],[103.835793,1.30709],[103.835757,1.30725],[103.83572,1.30741],[103.835,1.30753]
3,[103.83589,1.30653],[103.83587,1.306663],[103.83585,1.306797];1.3693





share|improve this answer




























    up vote
    2
    down vote













    With GNU sed 4.2.2, you'd use a simple sed script, like the following:



    sed 's/([0-9.]+) ([0-9.]+)/[2,1]/g;s/;/,/g' < input.csv > output.csv


    It needs backslashes for parentheses and + in the match pattern (as well as the numbers in the replacement) to make sed regard them as meta characters.



    The script consists of two exhaustive replacements for each line: firstly replacing two number-and-dot sequences separated by space with those sequences in opposite order, separated by comma and enclosed in brackets; secondly replacing semi-colon with comma.



    Though, with the dual use of , in the output, it might not be easily loaded as a CSV.






    share|improve this answer




























      up vote
      2
      down vote













      perl -F's+|;' -pale '$" = ",";
      $_ = "$F[0],[" . join("],[", map { "@F[2*$_,2*$_-1]" } 1 .. @F/2) . "]";
      ' yourfile


      Brief



      The field separator are set at multiple whitespaces or semicolon. Then
      excepting for the 1st field, the rest come in pairs. So keeping aside
      ($F[0]) we look at 2 fields at a time, flip them, & join all these pairs
      with "],[".





      share|improve this answer





















        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: true,
        showLowRepImageUploadWarning: true,
        reputationToPostImages: null,
        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%2funix.stackexchange.com%2fquestions%2f348824%2fswap-lat-lon-and-group-by-square-brackets%23new-answer', 'question_page');
        }
        );

        Post as a guest















        Required, but never shown

























        3 Answers
        3






        active

        oldest

        votes








        3 Answers
        3






        active

        oldest

        votes









        active

        oldest

        votes






        active

        oldest

        votes








        up vote
        2
        down vote













        sed 's/;([0-9.]*) ([0-9.]*)/,[2,1]/g' input.csv > output.csv


        can do the job but in 2nd line there is 1.306663 103.83587 1.30693 103.83583 group which has no ; separator. If it is not a misprint script should be modified as



        sed 's/[; ]?([0-9.]+) ([0-9.]+)/,[2,1]/g' input.csv > output.csv


        which produce



        1,[103.83589,1.30453],[103.83587,1.306663],[103.5,1.30],[103.83883,1.30693],[103.835793,1.30709],[103.835757,1.30725],[103.83572,1.30741],[103.8798,1.30753]
        2,[103.83589,1.353],[103.83587,1.306663],[103.83583,1.30693],[103.835793,1.30709],[103.835757,1.30725],[103.83572,1.30741],[103.835,1.30753]
        3,[103.83589,1.30653],[103.83587,1.306663],[103.83585,1.306797];1.3693





        share|improve this answer

























          up vote
          2
          down vote













          sed 's/;([0-9.]*) ([0-9.]*)/,[2,1]/g' input.csv > output.csv


          can do the job but in 2nd line there is 1.306663 103.83587 1.30693 103.83583 group which has no ; separator. If it is not a misprint script should be modified as



          sed 's/[; ]?([0-9.]+) ([0-9.]+)/,[2,1]/g' input.csv > output.csv


          which produce



          1,[103.83589,1.30453],[103.83587,1.306663],[103.5,1.30],[103.83883,1.30693],[103.835793,1.30709],[103.835757,1.30725],[103.83572,1.30741],[103.8798,1.30753]
          2,[103.83589,1.353],[103.83587,1.306663],[103.83583,1.30693],[103.835793,1.30709],[103.835757,1.30725],[103.83572,1.30741],[103.835,1.30753]
          3,[103.83589,1.30653],[103.83587,1.306663],[103.83585,1.306797];1.3693





          share|improve this answer























            up vote
            2
            down vote










            up vote
            2
            down vote









            sed 's/;([0-9.]*) ([0-9.]*)/,[2,1]/g' input.csv > output.csv


            can do the job but in 2nd line there is 1.306663 103.83587 1.30693 103.83583 group which has no ; separator. If it is not a misprint script should be modified as



            sed 's/[; ]?([0-9.]+) ([0-9.]+)/,[2,1]/g' input.csv > output.csv


            which produce



            1,[103.83589,1.30453],[103.83587,1.306663],[103.5,1.30],[103.83883,1.30693],[103.835793,1.30709],[103.835757,1.30725],[103.83572,1.30741],[103.8798,1.30753]
            2,[103.83589,1.353],[103.83587,1.306663],[103.83583,1.30693],[103.835793,1.30709],[103.835757,1.30725],[103.83572,1.30741],[103.835,1.30753]
            3,[103.83589,1.30653],[103.83587,1.306663],[103.83585,1.306797];1.3693





            share|improve this answer












            sed 's/;([0-9.]*) ([0-9.]*)/,[2,1]/g' input.csv > output.csv


            can do the job but in 2nd line there is 1.306663 103.83587 1.30693 103.83583 group which has no ; separator. If it is not a misprint script should be modified as



            sed 's/[; ]?([0-9.]+) ([0-9.]+)/,[2,1]/g' input.csv > output.csv


            which produce



            1,[103.83589,1.30453],[103.83587,1.306663],[103.5,1.30],[103.83883,1.30693],[103.835793,1.30709],[103.835757,1.30725],[103.83572,1.30741],[103.8798,1.30753]
            2,[103.83589,1.353],[103.83587,1.306663],[103.83583,1.30693],[103.835793,1.30709],[103.835757,1.30725],[103.83572,1.30741],[103.835,1.30753]
            3,[103.83589,1.30653],[103.83587,1.306663],[103.83585,1.306797];1.3693






            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Mar 3 '17 at 6:31









            Costas

            12.6k1129




            12.6k1129
























                up vote
                2
                down vote













                With GNU sed 4.2.2, you'd use a simple sed script, like the following:



                sed 's/([0-9.]+) ([0-9.]+)/[2,1]/g;s/;/,/g' < input.csv > output.csv


                It needs backslashes for parentheses and + in the match pattern (as well as the numbers in the replacement) to make sed regard them as meta characters.



                The script consists of two exhaustive replacements for each line: firstly replacing two number-and-dot sequences separated by space with those sequences in opposite order, separated by comma and enclosed in brackets; secondly replacing semi-colon with comma.



                Though, with the dual use of , in the output, it might not be easily loaded as a CSV.






                share|improve this answer

























                  up vote
                  2
                  down vote













                  With GNU sed 4.2.2, you'd use a simple sed script, like the following:



                  sed 's/([0-9.]+) ([0-9.]+)/[2,1]/g;s/;/,/g' < input.csv > output.csv


                  It needs backslashes for parentheses and + in the match pattern (as well as the numbers in the replacement) to make sed regard them as meta characters.



                  The script consists of two exhaustive replacements for each line: firstly replacing two number-and-dot sequences separated by space with those sequences in opposite order, separated by comma and enclosed in brackets; secondly replacing semi-colon with comma.



                  Though, with the dual use of , in the output, it might not be easily loaded as a CSV.






                  share|improve this answer























                    up vote
                    2
                    down vote










                    up vote
                    2
                    down vote









                    With GNU sed 4.2.2, you'd use a simple sed script, like the following:



                    sed 's/([0-9.]+) ([0-9.]+)/[2,1]/g;s/;/,/g' < input.csv > output.csv


                    It needs backslashes for parentheses and + in the match pattern (as well as the numbers in the replacement) to make sed regard them as meta characters.



                    The script consists of two exhaustive replacements for each line: firstly replacing two number-and-dot sequences separated by space with those sequences in opposite order, separated by comma and enclosed in brackets; secondly replacing semi-colon with comma.



                    Though, with the dual use of , in the output, it might not be easily loaded as a CSV.






                    share|improve this answer












                    With GNU sed 4.2.2, you'd use a simple sed script, like the following:



                    sed 's/([0-9.]+) ([0-9.]+)/[2,1]/g;s/;/,/g' < input.csv > output.csv


                    It needs backslashes for parentheses and + in the match pattern (as well as the numbers in the replacement) to make sed regard them as meta characters.



                    The script consists of two exhaustive replacements for each line: firstly replacing two number-and-dot sequences separated by space with those sequences in opposite order, separated by comma and enclosed in brackets; secondly replacing semi-colon with comma.



                    Though, with the dual use of , in the output, it might not be easily loaded as a CSV.







                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Mar 3 '17 at 6:40









                    Ralph Rönnquist

                    2,48238




                    2,48238






















                        up vote
                        2
                        down vote













                        perl -F's+|;' -pale '$" = ",";
                        $_ = "$F[0],[" . join("],[", map { "@F[2*$_,2*$_-1]" } 1 .. @F/2) . "]";
                        ' yourfile


                        Brief



                        The field separator are set at multiple whitespaces or semicolon. Then
                        excepting for the 1st field, the rest come in pairs. So keeping aside
                        ($F[0]) we look at 2 fields at a time, flip them, & join all these pairs
                        with "],[".





                        share|improve this answer

























                          up vote
                          2
                          down vote













                          perl -F's+|;' -pale '$" = ",";
                          $_ = "$F[0],[" . join("],[", map { "@F[2*$_,2*$_-1]" } 1 .. @F/2) . "]";
                          ' yourfile


                          Brief



                          The field separator are set at multiple whitespaces or semicolon. Then
                          excepting for the 1st field, the rest come in pairs. So keeping aside
                          ($F[0]) we look at 2 fields at a time, flip them, & join all these pairs
                          with "],[".





                          share|improve this answer























                            up vote
                            2
                            down vote










                            up vote
                            2
                            down vote









                            perl -F's+|;' -pale '$" = ",";
                            $_ = "$F[0],[" . join("],[", map { "@F[2*$_,2*$_-1]" } 1 .. @F/2) . "]";
                            ' yourfile


                            Brief



                            The field separator are set at multiple whitespaces or semicolon. Then
                            excepting for the 1st field, the rest come in pairs. So keeping aside
                            ($F[0]) we look at 2 fields at a time, flip them, & join all these pairs
                            with "],[".





                            share|improve this answer












                            perl -F's+|;' -pale '$" = ",";
                            $_ = "$F[0],[" . join("],[", map { "@F[2*$_,2*$_-1]" } 1 .. @F/2) . "]";
                            ' yourfile


                            Brief



                            The field separator are set at multiple whitespaces or semicolon. Then
                            excepting for the 1st field, the rest come in pairs. So keeping aside
                            ($F[0]) we look at 2 fields at a time, flip them, & join all these pairs
                            with "],[".






                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered Mar 3 '17 at 9:36







                            user218374





































                                 

                                draft saved


                                draft discarded



















































                                 


                                draft saved


                                draft discarded














                                StackExchange.ready(
                                function () {
                                StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f348824%2fswap-lat-lon-and-group-by-square-brackets%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

                                Accessing regular linux commands in Huawei's Dopra Linux

                                Can't connect RFCOMM socket: Host is down

                                Kernel panic - not syncing: Fatal Exception in Interrupt