Group rows in one file based on column value

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











up vote
3
down vote

favorite












I have "csv" spreadsheet as below



abc,12345,qwerty,A
xyz,12380,qwetty,R
abc,12389,qwerty,A
xyz,12324,qwetty,R


I would like to combine rows with similar values in columns 1, 2 and 4. Also, i would like to replace the last two alphabets of column 3 by "**". Sample of the output is as below:



abc,123**,qwerty,A
abc,123**,qwerty,A
xyz,123**,qwetty,R
xyz,123**,qwetty,R


Total number of lines is more than one Million!










share|improve this question









New contributor




Arpit is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.



















  • Hi @Arpit. You want to ` combine rows with similar values in columns 1, 2 and 4` or organize the rows` OR 'combine rows with similar values in columns 1 or 2 or 4`? thanks for any additional clarifications!
    – Goro
    7 hours ago










  • @Goro, i want to organize rows with similar value. Its re arranging rows with similar values.Thanks for correction.
    – Arpit
    7 hours ago










  • There is a difference between organizing the rows by one similar value or multiple similar values?
    – Goro
    7 hours ago










  • Yes. My requirement to organize based on multiple similar values. To be more specific, all rows with exactly same value in column 1,3 and 4 should be condition, based on which rows are arranged together. And then just replace last 2 digit of row 2 by *
    – Arpit
    7 hours ago










  • You can simply run the command sort on the file that you would like to organize, just sort file ;-)
    – Goro
    7 hours ago














up vote
3
down vote

favorite












I have "csv" spreadsheet as below



abc,12345,qwerty,A
xyz,12380,qwetty,R
abc,12389,qwerty,A
xyz,12324,qwetty,R


I would like to combine rows with similar values in columns 1, 2 and 4. Also, i would like to replace the last two alphabets of column 3 by "**". Sample of the output is as below:



abc,123**,qwerty,A
abc,123**,qwerty,A
xyz,123**,qwetty,R
xyz,123**,qwetty,R


Total number of lines is more than one Million!










share|improve this question









New contributor




Arpit is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.



















  • Hi @Arpit. You want to ` combine rows with similar values in columns 1, 2 and 4` or organize the rows` OR 'combine rows with similar values in columns 1 or 2 or 4`? thanks for any additional clarifications!
    – Goro
    7 hours ago










  • @Goro, i want to organize rows with similar value. Its re arranging rows with similar values.Thanks for correction.
    – Arpit
    7 hours ago










  • There is a difference between organizing the rows by one similar value or multiple similar values?
    – Goro
    7 hours ago










  • Yes. My requirement to organize based on multiple similar values. To be more specific, all rows with exactly same value in column 1,3 and 4 should be condition, based on which rows are arranged together. And then just replace last 2 digit of row 2 by *
    – Arpit
    7 hours ago










  • You can simply run the command sort on the file that you would like to organize, just sort file ;-)
    – Goro
    7 hours ago












up vote
3
down vote

favorite









up vote
3
down vote

favorite











I have "csv" spreadsheet as below



abc,12345,qwerty,A
xyz,12380,qwetty,R
abc,12389,qwerty,A
xyz,12324,qwetty,R


I would like to combine rows with similar values in columns 1, 2 and 4. Also, i would like to replace the last two alphabets of column 3 by "**". Sample of the output is as below:



abc,123**,qwerty,A
abc,123**,qwerty,A
xyz,123**,qwetty,R
xyz,123**,qwetty,R


Total number of lines is more than one Million!










share|improve this question









New contributor




Arpit is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











I have "csv" spreadsheet as below



abc,12345,qwerty,A
xyz,12380,qwetty,R
abc,12389,qwerty,A
xyz,12324,qwetty,R


I would like to combine rows with similar values in columns 1, 2 and 4. Also, i would like to replace the last two alphabets of column 3 by "**". Sample of the output is as below:



abc,123**,qwerty,A
abc,123**,qwerty,A
xyz,123**,qwetty,R
xyz,123**,qwetty,R


Total number of lines is more than one Million!







shell-script






share|improve this question









New contributor




Arpit is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











share|improve this question









New contributor




Arpit is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









share|improve this question




share|improve this question








edited 7 hours ago









RalfFriedl

3,7351623




3,7351623






New contributor




Arpit is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









asked 7 hours ago









Arpit

161




161




New contributor




Arpit is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.





New contributor





Arpit is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.






Arpit is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











  • Hi @Arpit. You want to ` combine rows with similar values in columns 1, 2 and 4` or organize the rows` OR 'combine rows with similar values in columns 1 or 2 or 4`? thanks for any additional clarifications!
    – Goro
    7 hours ago










  • @Goro, i want to organize rows with similar value. Its re arranging rows with similar values.Thanks for correction.
    – Arpit
    7 hours ago










  • There is a difference between organizing the rows by one similar value or multiple similar values?
    – Goro
    7 hours ago










  • Yes. My requirement to organize based on multiple similar values. To be more specific, all rows with exactly same value in column 1,3 and 4 should be condition, based on which rows are arranged together. And then just replace last 2 digit of row 2 by *
    – Arpit
    7 hours ago










  • You can simply run the command sort on the file that you would like to organize, just sort file ;-)
    – Goro
    7 hours ago
















  • Hi @Arpit. You want to ` combine rows with similar values in columns 1, 2 and 4` or organize the rows` OR 'combine rows with similar values in columns 1 or 2 or 4`? thanks for any additional clarifications!
    – Goro
    7 hours ago










  • @Goro, i want to organize rows with similar value. Its re arranging rows with similar values.Thanks for correction.
    – Arpit
    7 hours ago










  • There is a difference between organizing the rows by one similar value or multiple similar values?
    – Goro
    7 hours ago










  • Yes. My requirement to organize based on multiple similar values. To be more specific, all rows with exactly same value in column 1,3 and 4 should be condition, based on which rows are arranged together. And then just replace last 2 digit of row 2 by *
    – Arpit
    7 hours ago










  • You can simply run the command sort on the file that you would like to organize, just sort file ;-)
    – Goro
    7 hours ago















Hi @Arpit. You want to ` combine rows with similar values in columns 1, 2 and 4` or organize the rows` OR 'combine rows with similar values in columns 1 or 2 or 4`? thanks for any additional clarifications!
– Goro
7 hours ago




Hi @Arpit. You want to ` combine rows with similar values in columns 1, 2 and 4` or organize the rows` OR 'combine rows with similar values in columns 1 or 2 or 4`? thanks for any additional clarifications!
– Goro
7 hours ago












@Goro, i want to organize rows with similar value. Its re arranging rows with similar values.Thanks for correction.
– Arpit
7 hours ago




@Goro, i want to organize rows with similar value. Its re arranging rows with similar values.Thanks for correction.
– Arpit
7 hours ago












There is a difference between organizing the rows by one similar value or multiple similar values?
– Goro
7 hours ago




There is a difference between organizing the rows by one similar value or multiple similar values?
– Goro
7 hours ago












Yes. My requirement to organize based on multiple similar values. To be more specific, all rows with exactly same value in column 1,3 and 4 should be condition, based on which rows are arranged together. And then just replace last 2 digit of row 2 by *
– Arpit
7 hours ago




Yes. My requirement to organize based on multiple similar values. To be more specific, all rows with exactly same value in column 1,3 and 4 should be condition, based on which rows are arranged together. And then just replace last 2 digit of row 2 by *
– Arpit
7 hours ago












You can simply run the command sort on the file that you would like to organize, just sort file ;-)
– Goro
7 hours ago




You can simply run the command sort on the file that you would like to organize, just sort file ;-)
– Goro
7 hours ago










2 Answers
2






active

oldest

votes

















up vote
5
down vote













To sort your file first by col1 then by col2 then by col4:



$ sort -t, -k1,1 -k2,2 -k4,4 file
abc,12345,qwerty,A
abc,12389,qwerty,A
xyz,12324,qwetty,R
xyz,12380,qwetty,R


Then to obfuscate the 2nd field, you could do



$ sort -t, -k1,1 -k2,2 -k4,4 file | sed 's/..,/**,/2'
abc,123**,qwerty,A
abc,123**,qwerty,A
xyz,123**,qwetty,R
xyz,123**,qwetty,R





share|improve this answer



























    up vote
    4
    down vote













    sort file | awk -F',' ' sub(/..$/,"**",$2) 1' OFS=','
    abc,123**,qwerty,A
    abc,123**,qwerty,A
    xyz,123**,qwetty,R
    xyz,123**,qwetty,R


    In case you would like to change the comma separator to tab. you can use this command:



    sort file | sed 's/,/t/g' | awk -F't' ' sub(/..$/,"**",$2) 1' OFS='t'
    abc 123** qwerty A
    abc 123** qwerty A
    xyz 123** qwetty R
    xyz 123** qwetty R





    share|improve this answer


















    • 2




      why are you changing the separator to tab? The question seems pretty clear about commas.
      – glenn jackman
      4 hours ago










    • @glenn jackman. Correct! It seems I missed the first part when wrote the answer quickly. Thank you for catching that! ;-)
      – Goro
      4 hours ago










    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
    );



    );






    Arpit is a new contributor. Be nice, and check out our Code of Conduct.









     

    draft saved


    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f470335%2fgroup-rows-in-one-file-based-on-column-value%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
    5
    down vote













    To sort your file first by col1 then by col2 then by col4:



    $ sort -t, -k1,1 -k2,2 -k4,4 file
    abc,12345,qwerty,A
    abc,12389,qwerty,A
    xyz,12324,qwetty,R
    xyz,12380,qwetty,R


    Then to obfuscate the 2nd field, you could do



    $ sort -t, -k1,1 -k2,2 -k4,4 file | sed 's/..,/**,/2'
    abc,123**,qwerty,A
    abc,123**,qwerty,A
    xyz,123**,qwetty,R
    xyz,123**,qwetty,R





    share|improve this answer
























      up vote
      5
      down vote













      To sort your file first by col1 then by col2 then by col4:



      $ sort -t, -k1,1 -k2,2 -k4,4 file
      abc,12345,qwerty,A
      abc,12389,qwerty,A
      xyz,12324,qwetty,R
      xyz,12380,qwetty,R


      Then to obfuscate the 2nd field, you could do



      $ sort -t, -k1,1 -k2,2 -k4,4 file | sed 's/..,/**,/2'
      abc,123**,qwerty,A
      abc,123**,qwerty,A
      xyz,123**,qwetty,R
      xyz,123**,qwetty,R





      share|improve this answer






















        up vote
        5
        down vote










        up vote
        5
        down vote









        To sort your file first by col1 then by col2 then by col4:



        $ sort -t, -k1,1 -k2,2 -k4,4 file
        abc,12345,qwerty,A
        abc,12389,qwerty,A
        xyz,12324,qwetty,R
        xyz,12380,qwetty,R


        Then to obfuscate the 2nd field, you could do



        $ sort -t, -k1,1 -k2,2 -k4,4 file | sed 's/..,/**,/2'
        abc,123**,qwerty,A
        abc,123**,qwerty,A
        xyz,123**,qwetty,R
        xyz,123**,qwetty,R





        share|improve this answer












        To sort your file first by col1 then by col2 then by col4:



        $ sort -t, -k1,1 -k2,2 -k4,4 file
        abc,12345,qwerty,A
        abc,12389,qwerty,A
        xyz,12324,qwetty,R
        xyz,12380,qwetty,R


        Then to obfuscate the 2nd field, you could do



        $ sort -t, -k1,1 -k2,2 -k4,4 file | sed 's/..,/**,/2'
        abc,123**,qwerty,A
        abc,123**,qwerty,A
        xyz,123**,qwetty,R
        xyz,123**,qwetty,R






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered 6 hours ago









        glenn jackman

        47.7k265104




        47.7k265104






















            up vote
            4
            down vote













            sort file | awk -F',' ' sub(/..$/,"**",$2) 1' OFS=','
            abc,123**,qwerty,A
            abc,123**,qwerty,A
            xyz,123**,qwetty,R
            xyz,123**,qwetty,R


            In case you would like to change the comma separator to tab. you can use this command:



            sort file | sed 's/,/t/g' | awk -F't' ' sub(/..$/,"**",$2) 1' OFS='t'
            abc 123** qwerty A
            abc 123** qwerty A
            xyz 123** qwetty R
            xyz 123** qwetty R





            share|improve this answer


















            • 2




              why are you changing the separator to tab? The question seems pretty clear about commas.
              – glenn jackman
              4 hours ago










            • @glenn jackman. Correct! It seems I missed the first part when wrote the answer quickly. Thank you for catching that! ;-)
              – Goro
              4 hours ago














            up vote
            4
            down vote













            sort file | awk -F',' ' sub(/..$/,"**",$2) 1' OFS=','
            abc,123**,qwerty,A
            abc,123**,qwerty,A
            xyz,123**,qwetty,R
            xyz,123**,qwetty,R


            In case you would like to change the comma separator to tab. you can use this command:



            sort file | sed 's/,/t/g' | awk -F't' ' sub(/..$/,"**",$2) 1' OFS='t'
            abc 123** qwerty A
            abc 123** qwerty A
            xyz 123** qwetty R
            xyz 123** qwetty R





            share|improve this answer


















            • 2




              why are you changing the separator to tab? The question seems pretty clear about commas.
              – glenn jackman
              4 hours ago










            • @glenn jackman. Correct! It seems I missed the first part when wrote the answer quickly. Thank you for catching that! ;-)
              – Goro
              4 hours ago












            up vote
            4
            down vote










            up vote
            4
            down vote









            sort file | awk -F',' ' sub(/..$/,"**",$2) 1' OFS=','
            abc,123**,qwerty,A
            abc,123**,qwerty,A
            xyz,123**,qwetty,R
            xyz,123**,qwetty,R


            In case you would like to change the comma separator to tab. you can use this command:



            sort file | sed 's/,/t/g' | awk -F't' ' sub(/..$/,"**",$2) 1' OFS='t'
            abc 123** qwerty A
            abc 123** qwerty A
            xyz 123** qwetty R
            xyz 123** qwetty R





            share|improve this answer














            sort file | awk -F',' ' sub(/..$/,"**",$2) 1' OFS=','
            abc,123**,qwerty,A
            abc,123**,qwerty,A
            xyz,123**,qwetty,R
            xyz,123**,qwetty,R


            In case you would like to change the comma separator to tab. you can use this command:



            sort file | sed 's/,/t/g' | awk -F't' ' sub(/..$/,"**",$2) 1' OFS='t'
            abc 123** qwerty A
            abc 123** qwerty A
            xyz 123** qwetty R
            xyz 123** qwetty R






            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited 4 hours ago

























            answered 6 hours ago









            Goro

            3,37441951




            3,37441951







            • 2




              why are you changing the separator to tab? The question seems pretty clear about commas.
              – glenn jackman
              4 hours ago










            • @glenn jackman. Correct! It seems I missed the first part when wrote the answer quickly. Thank you for catching that! ;-)
              – Goro
              4 hours ago












            • 2




              why are you changing the separator to tab? The question seems pretty clear about commas.
              – glenn jackman
              4 hours ago










            • @glenn jackman. Correct! It seems I missed the first part when wrote the answer quickly. Thank you for catching that! ;-)
              – Goro
              4 hours ago







            2




            2




            why are you changing the separator to tab? The question seems pretty clear about commas.
            – glenn jackman
            4 hours ago




            why are you changing the separator to tab? The question seems pretty clear about commas.
            – glenn jackman
            4 hours ago












            @glenn jackman. Correct! It seems I missed the first part when wrote the answer quickly. Thank you for catching that! ;-)
            – Goro
            4 hours ago




            @glenn jackman. Correct! It seems I missed the first part when wrote the answer quickly. Thank you for catching that! ;-)
            – Goro
            4 hours ago










            Arpit is a new contributor. Be nice, and check out our Code of Conduct.









             

            draft saved


            draft discarded


















            Arpit is a new contributor. Be nice, and check out our Code of Conduct.












            Arpit is a new contributor. Be nice, and check out our Code of Conduct.











            Arpit is a new contributor. Be nice, and check out our Code of Conduct.













             


            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f470335%2fgroup-rows-in-one-file-based-on-column-value%23new-answer', 'question_page');

            );

            Post as a guest













































































            Comments

            Popular posts from this blog

            Long meetings (6-7 hours a day): Being “babysat” by supervisor

            What does second last employer means? [closed]

            One-line joke