group lines according to first word












4















How can I modify the following content of a file:



cat:persian/young-1
cat:winter/young-2
cat:summer/wild-3
dog:persian/young-1
dog:winter/young-2
dog:summer/wild-3


To :



cat:persian/young-1
cat:winter/young-2
cat:summer/wild-3

dog:persian/young-1
dog:winter/young-2
dog:summer/wild-3


It's not specific to dog or cat, it's more of symbolic representation of whatever the first word/term is










share|improve this question

























  • Will the first word always be delimited by a colon?

    – Jesse_b
    Jan 25 at 14:30











  • Yes it'll always be in cat: or dog: format but a solution to something without being delimited by a colon is also welcomed @Jesse_b

    – user607694
    Jan 25 at 14:33













  • Please clarify whether the blank line in the output is required or cosmetic.

    – agc
    Jan 25 at 14:55













  • @agc: The blank line is really the only change between input and desired output.

    – Jesse_b
    Jan 25 at 15:21






  • 1





    uniq can do it, but you have to hardcode the width of the first field, which makes it frustratingly useless: uniq --group -w 4 file

    – glenn jackman
    Jan 25 at 16:30
















4















How can I modify the following content of a file:



cat:persian/young-1
cat:winter/young-2
cat:summer/wild-3
dog:persian/young-1
dog:winter/young-2
dog:summer/wild-3


To :



cat:persian/young-1
cat:winter/young-2
cat:summer/wild-3

dog:persian/young-1
dog:winter/young-2
dog:summer/wild-3


It's not specific to dog or cat, it's more of symbolic representation of whatever the first word/term is










share|improve this question

























  • Will the first word always be delimited by a colon?

    – Jesse_b
    Jan 25 at 14:30











  • Yes it'll always be in cat: or dog: format but a solution to something without being delimited by a colon is also welcomed @Jesse_b

    – user607694
    Jan 25 at 14:33













  • Please clarify whether the blank line in the output is required or cosmetic.

    – agc
    Jan 25 at 14:55













  • @agc: The blank line is really the only change between input and desired output.

    – Jesse_b
    Jan 25 at 15:21






  • 1





    uniq can do it, but you have to hardcode the width of the first field, which makes it frustratingly useless: uniq --group -w 4 file

    – glenn jackman
    Jan 25 at 16:30














4












4








4


1






How can I modify the following content of a file:



cat:persian/young-1
cat:winter/young-2
cat:summer/wild-3
dog:persian/young-1
dog:winter/young-2
dog:summer/wild-3


To :



cat:persian/young-1
cat:winter/young-2
cat:summer/wild-3

dog:persian/young-1
dog:winter/young-2
dog:summer/wild-3


It's not specific to dog or cat, it's more of symbolic representation of whatever the first word/term is










share|improve this question
















How can I modify the following content of a file:



cat:persian/young-1
cat:winter/young-2
cat:summer/wild-3
dog:persian/young-1
dog:winter/young-2
dog:summer/wild-3


To :



cat:persian/young-1
cat:winter/young-2
cat:summer/wild-3

dog:persian/young-1
dog:winter/young-2
dog:summer/wild-3


It's not specific to dog or cat, it's more of symbolic representation of whatever the first word/term is







text-processing awk sed grep






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jan 25 at 16:18









steve

14.1k22552




14.1k22552










asked Jan 25 at 14:27









user607694user607694

504




504













  • Will the first word always be delimited by a colon?

    – Jesse_b
    Jan 25 at 14:30











  • Yes it'll always be in cat: or dog: format but a solution to something without being delimited by a colon is also welcomed @Jesse_b

    – user607694
    Jan 25 at 14:33













  • Please clarify whether the blank line in the output is required or cosmetic.

    – agc
    Jan 25 at 14:55













  • @agc: The blank line is really the only change between input and desired output.

    – Jesse_b
    Jan 25 at 15:21






  • 1





    uniq can do it, but you have to hardcode the width of the first field, which makes it frustratingly useless: uniq --group -w 4 file

    – glenn jackman
    Jan 25 at 16:30



















  • Will the first word always be delimited by a colon?

    – Jesse_b
    Jan 25 at 14:30











  • Yes it'll always be in cat: or dog: format but a solution to something without being delimited by a colon is also welcomed @Jesse_b

    – user607694
    Jan 25 at 14:33













  • Please clarify whether the blank line in the output is required or cosmetic.

    – agc
    Jan 25 at 14:55













  • @agc: The blank line is really the only change between input and desired output.

    – Jesse_b
    Jan 25 at 15:21






  • 1





    uniq can do it, but you have to hardcode the width of the first field, which makes it frustratingly useless: uniq --group -w 4 file

    – glenn jackman
    Jan 25 at 16:30

















Will the first word always be delimited by a colon?

– Jesse_b
Jan 25 at 14:30





Will the first word always be delimited by a colon?

– Jesse_b
Jan 25 at 14:30













Yes it'll always be in cat: or dog: format but a solution to something without being delimited by a colon is also welcomed @Jesse_b

– user607694
Jan 25 at 14:33







Yes it'll always be in cat: or dog: format but a solution to something without being delimited by a colon is also welcomed @Jesse_b

– user607694
Jan 25 at 14:33















Please clarify whether the blank line in the output is required or cosmetic.

– agc
Jan 25 at 14:55







Please clarify whether the blank line in the output is required or cosmetic.

– agc
Jan 25 at 14:55















@agc: The blank line is really the only change between input and desired output.

– Jesse_b
Jan 25 at 15:21





@agc: The blank line is really the only change between input and desired output.

– Jesse_b
Jan 25 at 15:21




1




1





uniq can do it, but you have to hardcode the width of the first field, which makes it frustratingly useless: uniq --group -w 4 file

– glenn jackman
Jan 25 at 16:30





uniq can do it, but you have to hardcode the width of the first field, which makes it frustratingly useless: uniq --group -w 4 file

– glenn jackman
Jan 25 at 16:30










5 Answers
5






active

oldest

votes


















10














You could do something like:



awk -F: 'NR>1 && $1 "" != last {print ""}; {print; last = $1}'


The "" is to force string comparison. Without it, it wouldn't work properly in input like:



100:foo
100:bar
1e2:baz
1e2:biz


Where 100 and 1e2 would be compared as numbers.






share|improve this answer































    7














    Here's one way. If the first field isn't the same as the one in the previous line, print a break....



    $ awk -F: '$1!=a&&a{print ""}{a=$1}1' myfile
    cat:persian/young-1
    cat:winter/young-2
    cat:summer/wild-3

    dog:persian/young-1
    dog:winter/young-2
    dog:summer/wild-3
    $


    Explanation:





    • -F: = set the field delimiter to :


    • $1!=a&&a = if first field is not equal to variable "a" (the previous first field), and variable "a" is set to something (i.e. we're not dealing with the very first line in the file)


    • {print ""} = print a blank line


    • {a=$1} = for every line read, set variable "a" to the first field


    • 1 = print the line






    share|improve this answer


























    • and variable "a" is set to something is actually "a" is set no a non-empty string that is none of the representations of the zero number (like "0", "0e2", " 0 ", 1e-500... possibly 0x0, 0x0p4... with some implementations)

      – Stéphane Chazelas
      Jan 25 at 17:00





















    1














    I tried with this way



    en ~]# awk '/cat/{print $0}' filename| sed '$s/.*/&n/g';awk '/dog/{print $0}' filename

    cat:persian/young-1
    cat:winter/young-2
    cat:summer/wild-3

    dog:persian/young-1
    dog:winter/young-2
    dog:summer/wild-3
    [root@praveen ~]#





    share|improve this answer
























    • needs a tweak to avoid that first blank line of output, in order to match the desired reult ?

      – steve
      Jan 25 at 16:09











    • @steve: That's easy, just pipe it through tail -n +2. Nobody said you have to do everything in awk.

      – Kevin
      Jan 26 at 2:32



















    1














    You could employ the sed editor for this job too:



    $ sed -e '
    $q;N
    P
    /^([^:]*):.*n1:/!{x;p;x;}
    D
    ' input

    cat:persian/young-1
    cat:winter/young-2
    cat:summer/wild-3

    dog:persian/young-1
    dog:winter/young-2
    dog:summer/wild-3


    Basic idea is to have 2 lines at any time in the pattern space. Compare the strings before the leading colons ":" in the pattern space. Should they not match, means a
    changeover of an animal is detected, and we promptly display an empty line.






    share|improve this answer































      0














      It could be done with awk:



      awk -F: ' $0!="" && last!="" && $1!=last"" {print""} {last=$1} 1'



      • -F : used to split the fields on a : character.


      • $0 != "" is required to avoid converting one empty line into three.

        This allows re-processing an already processed file without adding empty lines.


      • last != "" is required to avoid the file first line (where last is empty).



      • $1 != last"" compare the present first field to the previous one.

        The trailing "" ensure that the comparison is done in text mode. Otherwise, equivalent (to awk) numeric values will be seen as equal, like:



        7:first line
        7e0:second line
        7.0000:third line
        7.000000000000000000008:fourth line


      • {print""} print an empty line (if all the tests above match).


      • {last=$1} store the line first field in the variable last.


      • 1 print the line contents (always).







      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',
        autoActivateHeartbeat: false,
        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%2f496684%2fgroup-lines-according-to-first-word%23new-answer', 'question_page');
        }
        );

        Post as a guest















        Required, but never shown

























        5 Answers
        5






        active

        oldest

        votes








        5 Answers
        5






        active

        oldest

        votes









        active

        oldest

        votes






        active

        oldest

        votes









        10














        You could do something like:



        awk -F: 'NR>1 && $1 "" != last {print ""}; {print; last = $1}'


        The "" is to force string comparison. Without it, it wouldn't work properly in input like:



        100:foo
        100:bar
        1e2:baz
        1e2:biz


        Where 100 and 1e2 would be compared as numbers.






        share|improve this answer




























          10














          You could do something like:



          awk -F: 'NR>1 && $1 "" != last {print ""}; {print; last = $1}'


          The "" is to force string comparison. Without it, it wouldn't work properly in input like:



          100:foo
          100:bar
          1e2:baz
          1e2:biz


          Where 100 and 1e2 would be compared as numbers.






          share|improve this answer


























            10












            10








            10







            You could do something like:



            awk -F: 'NR>1 && $1 "" != last {print ""}; {print; last = $1}'


            The "" is to force string comparison. Without it, it wouldn't work properly in input like:



            100:foo
            100:bar
            1e2:baz
            1e2:biz


            Where 100 and 1e2 would be compared as numbers.






            share|improve this answer













            You could do something like:



            awk -F: 'NR>1 && $1 "" != last {print ""}; {print; last = $1}'


            The "" is to force string comparison. Without it, it wouldn't work properly in input like:



            100:foo
            100:bar
            1e2:baz
            1e2:biz


            Where 100 and 1e2 would be compared as numbers.







            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Jan 25 at 14:43









            Stéphane ChazelasStéphane Chazelas

            309k57583945




            309k57583945

























                7














                Here's one way. If the first field isn't the same as the one in the previous line, print a break....



                $ awk -F: '$1!=a&&a{print ""}{a=$1}1' myfile
                cat:persian/young-1
                cat:winter/young-2
                cat:summer/wild-3

                dog:persian/young-1
                dog:winter/young-2
                dog:summer/wild-3
                $


                Explanation:





                • -F: = set the field delimiter to :


                • $1!=a&&a = if first field is not equal to variable "a" (the previous first field), and variable "a" is set to something (i.e. we're not dealing with the very first line in the file)


                • {print ""} = print a blank line


                • {a=$1} = for every line read, set variable "a" to the first field


                • 1 = print the line






                share|improve this answer


























                • and variable "a" is set to something is actually "a" is set no a non-empty string that is none of the representations of the zero number (like "0", "0e2", " 0 ", 1e-500... possibly 0x0, 0x0p4... with some implementations)

                  – Stéphane Chazelas
                  Jan 25 at 17:00


















                7














                Here's one way. If the first field isn't the same as the one in the previous line, print a break....



                $ awk -F: '$1!=a&&a{print ""}{a=$1}1' myfile
                cat:persian/young-1
                cat:winter/young-2
                cat:summer/wild-3

                dog:persian/young-1
                dog:winter/young-2
                dog:summer/wild-3
                $


                Explanation:





                • -F: = set the field delimiter to :


                • $1!=a&&a = if first field is not equal to variable "a" (the previous first field), and variable "a" is set to something (i.e. we're not dealing with the very first line in the file)


                • {print ""} = print a blank line


                • {a=$1} = for every line read, set variable "a" to the first field


                • 1 = print the line






                share|improve this answer


























                • and variable "a" is set to something is actually "a" is set no a non-empty string that is none of the representations of the zero number (like "0", "0e2", " 0 ", 1e-500... possibly 0x0, 0x0p4... with some implementations)

                  – Stéphane Chazelas
                  Jan 25 at 17:00
















                7












                7








                7







                Here's one way. If the first field isn't the same as the one in the previous line, print a break....



                $ awk -F: '$1!=a&&a{print ""}{a=$1}1' myfile
                cat:persian/young-1
                cat:winter/young-2
                cat:summer/wild-3

                dog:persian/young-1
                dog:winter/young-2
                dog:summer/wild-3
                $


                Explanation:





                • -F: = set the field delimiter to :


                • $1!=a&&a = if first field is not equal to variable "a" (the previous first field), and variable "a" is set to something (i.e. we're not dealing with the very first line in the file)


                • {print ""} = print a blank line


                • {a=$1} = for every line read, set variable "a" to the first field


                • 1 = print the line






                share|improve this answer















                Here's one way. If the first field isn't the same as the one in the previous line, print a break....



                $ awk -F: '$1!=a&&a{print ""}{a=$1}1' myfile
                cat:persian/young-1
                cat:winter/young-2
                cat:summer/wild-3

                dog:persian/young-1
                dog:winter/young-2
                dog:summer/wild-3
                $


                Explanation:





                • -F: = set the field delimiter to :


                • $1!=a&&a = if first field is not equal to variable "a" (the previous first field), and variable "a" is set to something (i.e. we're not dealing with the very first line in the file)


                • {print ""} = print a blank line


                • {a=$1} = for every line read, set variable "a" to the first field


                • 1 = print the line







                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited Jan 25 at 16:05

























                answered Jan 25 at 14:47









                stevesteve

                14.1k22552




                14.1k22552













                • and variable "a" is set to something is actually "a" is set no a non-empty string that is none of the representations of the zero number (like "0", "0e2", " 0 ", 1e-500... possibly 0x0, 0x0p4... with some implementations)

                  – Stéphane Chazelas
                  Jan 25 at 17:00





















                • and variable "a" is set to something is actually "a" is set no a non-empty string that is none of the representations of the zero number (like "0", "0e2", " 0 ", 1e-500... possibly 0x0, 0x0p4... with some implementations)

                  – Stéphane Chazelas
                  Jan 25 at 17:00



















                and variable "a" is set to something is actually "a" is set no a non-empty string that is none of the representations of the zero number (like "0", "0e2", " 0 ", 1e-500... possibly 0x0, 0x0p4... with some implementations)

                – Stéphane Chazelas
                Jan 25 at 17:00







                and variable "a" is set to something is actually "a" is set no a non-empty string that is none of the representations of the zero number (like "0", "0e2", " 0 ", 1e-500... possibly 0x0, 0x0p4... with some implementations)

                – Stéphane Chazelas
                Jan 25 at 17:00













                1














                I tried with this way



                en ~]# awk '/cat/{print $0}' filename| sed '$s/.*/&n/g';awk '/dog/{print $0}' filename

                cat:persian/young-1
                cat:winter/young-2
                cat:summer/wild-3

                dog:persian/young-1
                dog:winter/young-2
                dog:summer/wild-3
                [root@praveen ~]#





                share|improve this answer
























                • needs a tweak to avoid that first blank line of output, in order to match the desired reult ?

                  – steve
                  Jan 25 at 16:09











                • @steve: That's easy, just pipe it through tail -n +2. Nobody said you have to do everything in awk.

                  – Kevin
                  Jan 26 at 2:32
















                1














                I tried with this way



                en ~]# awk '/cat/{print $0}' filename| sed '$s/.*/&n/g';awk '/dog/{print $0}' filename

                cat:persian/young-1
                cat:winter/young-2
                cat:summer/wild-3

                dog:persian/young-1
                dog:winter/young-2
                dog:summer/wild-3
                [root@praveen ~]#





                share|improve this answer
























                • needs a tweak to avoid that first blank line of output, in order to match the desired reult ?

                  – steve
                  Jan 25 at 16:09











                • @steve: That's easy, just pipe it through tail -n +2. Nobody said you have to do everything in awk.

                  – Kevin
                  Jan 26 at 2:32














                1












                1








                1







                I tried with this way



                en ~]# awk '/cat/{print $0}' filename| sed '$s/.*/&n/g';awk '/dog/{print $0}' filename

                cat:persian/young-1
                cat:winter/young-2
                cat:summer/wild-3

                dog:persian/young-1
                dog:winter/young-2
                dog:summer/wild-3
                [root@praveen ~]#





                share|improve this answer













                I tried with this way



                en ~]# awk '/cat/{print $0}' filename| sed '$s/.*/&n/g';awk '/dog/{print $0}' filename

                cat:persian/young-1
                cat:winter/young-2
                cat:summer/wild-3

                dog:persian/young-1
                dog:winter/young-2
                dog:summer/wild-3
                [root@praveen ~]#






                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Jan 25 at 16:07









                Praveen Kumar BSPraveen Kumar BS

                1,5261311




                1,5261311













                • needs a tweak to avoid that first blank line of output, in order to match the desired reult ?

                  – steve
                  Jan 25 at 16:09











                • @steve: That's easy, just pipe it through tail -n +2. Nobody said you have to do everything in awk.

                  – Kevin
                  Jan 26 at 2:32



















                • needs a tweak to avoid that first blank line of output, in order to match the desired reult ?

                  – steve
                  Jan 25 at 16:09











                • @steve: That's easy, just pipe it through tail -n +2. Nobody said you have to do everything in awk.

                  – Kevin
                  Jan 26 at 2:32

















                needs a tweak to avoid that first blank line of output, in order to match the desired reult ?

                – steve
                Jan 25 at 16:09





                needs a tweak to avoid that first blank line of output, in order to match the desired reult ?

                – steve
                Jan 25 at 16:09













                @steve: That's easy, just pipe it through tail -n +2. Nobody said you have to do everything in awk.

                – Kevin
                Jan 26 at 2:32





                @steve: That's easy, just pipe it through tail -n +2. Nobody said you have to do everything in awk.

                – Kevin
                Jan 26 at 2:32











                1














                You could employ the sed editor for this job too:



                $ sed -e '
                $q;N
                P
                /^([^:]*):.*n1:/!{x;p;x;}
                D
                ' input

                cat:persian/young-1
                cat:winter/young-2
                cat:summer/wild-3

                dog:persian/young-1
                dog:winter/young-2
                dog:summer/wild-3


                Basic idea is to have 2 lines at any time in the pattern space. Compare the strings before the leading colons ":" in the pattern space. Should they not match, means a
                changeover of an animal is detected, and we promptly display an empty line.






                share|improve this answer




























                  1














                  You could employ the sed editor for this job too:



                  $ sed -e '
                  $q;N
                  P
                  /^([^:]*):.*n1:/!{x;p;x;}
                  D
                  ' input

                  cat:persian/young-1
                  cat:winter/young-2
                  cat:summer/wild-3

                  dog:persian/young-1
                  dog:winter/young-2
                  dog:summer/wild-3


                  Basic idea is to have 2 lines at any time in the pattern space. Compare the strings before the leading colons ":" in the pattern space. Should they not match, means a
                  changeover of an animal is detected, and we promptly display an empty line.






                  share|improve this answer


























                    1












                    1








                    1







                    You could employ the sed editor for this job too:



                    $ sed -e '
                    $q;N
                    P
                    /^([^:]*):.*n1:/!{x;p;x;}
                    D
                    ' input

                    cat:persian/young-1
                    cat:winter/young-2
                    cat:summer/wild-3

                    dog:persian/young-1
                    dog:winter/young-2
                    dog:summer/wild-3


                    Basic idea is to have 2 lines at any time in the pattern space. Compare the strings before the leading colons ":" in the pattern space. Should they not match, means a
                    changeover of an animal is detected, and we promptly display an empty line.






                    share|improve this answer













                    You could employ the sed editor for this job too:



                    $ sed -e '
                    $q;N
                    P
                    /^([^:]*):.*n1:/!{x;p;x;}
                    D
                    ' input

                    cat:persian/young-1
                    cat:winter/young-2
                    cat:summer/wild-3

                    dog:persian/young-1
                    dog:winter/young-2
                    dog:summer/wild-3


                    Basic idea is to have 2 lines at any time in the pattern space. Compare the strings before the leading colons ":" in the pattern space. Should they not match, means a
                    changeover of an animal is detected, and we promptly display an empty line.







                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Jan 26 at 11:01









                    Rakesh SharmaRakesh Sharma

                    582




                    582























                        0














                        It could be done with awk:



                        awk -F: ' $0!="" && last!="" && $1!=last"" {print""} {last=$1} 1'



                        • -F : used to split the fields on a : character.


                        • $0 != "" is required to avoid converting one empty line into three.

                          This allows re-processing an already processed file without adding empty lines.


                        • last != "" is required to avoid the file first line (where last is empty).



                        • $1 != last"" compare the present first field to the previous one.

                          The trailing "" ensure that the comparison is done in text mode. Otherwise, equivalent (to awk) numeric values will be seen as equal, like:



                          7:first line
                          7e0:second line
                          7.0000:third line
                          7.000000000000000000008:fourth line


                        • {print""} print an empty line (if all the tests above match).


                        • {last=$1} store the line first field in the variable last.


                        • 1 print the line contents (always).







                        share|improve this answer




























                          0














                          It could be done with awk:



                          awk -F: ' $0!="" && last!="" && $1!=last"" {print""} {last=$1} 1'



                          • -F : used to split the fields on a : character.


                          • $0 != "" is required to avoid converting one empty line into three.

                            This allows re-processing an already processed file without adding empty lines.


                          • last != "" is required to avoid the file first line (where last is empty).



                          • $1 != last"" compare the present first field to the previous one.

                            The trailing "" ensure that the comparison is done in text mode. Otherwise, equivalent (to awk) numeric values will be seen as equal, like:



                            7:first line
                            7e0:second line
                            7.0000:third line
                            7.000000000000000000008:fourth line


                          • {print""} print an empty line (if all the tests above match).


                          • {last=$1} store the line first field in the variable last.


                          • 1 print the line contents (always).







                          share|improve this answer


























                            0












                            0








                            0







                            It could be done with awk:



                            awk -F: ' $0!="" && last!="" && $1!=last"" {print""} {last=$1} 1'



                            • -F : used to split the fields on a : character.


                            • $0 != "" is required to avoid converting one empty line into three.

                              This allows re-processing an already processed file without adding empty lines.


                            • last != "" is required to avoid the file first line (where last is empty).



                            • $1 != last"" compare the present first field to the previous one.

                              The trailing "" ensure that the comparison is done in text mode. Otherwise, equivalent (to awk) numeric values will be seen as equal, like:



                              7:first line
                              7e0:second line
                              7.0000:third line
                              7.000000000000000000008:fourth line


                            • {print""} print an empty line (if all the tests above match).


                            • {last=$1} store the line first field in the variable last.


                            • 1 print the line contents (always).







                            share|improve this answer













                            It could be done with awk:



                            awk -F: ' $0!="" && last!="" && $1!=last"" {print""} {last=$1} 1'



                            • -F : used to split the fields on a : character.


                            • $0 != "" is required to avoid converting one empty line into three.

                              This allows re-processing an already processed file without adding empty lines.


                            • last != "" is required to avoid the file first line (where last is empty).



                            • $1 != last"" compare the present first field to the previous one.

                              The trailing "" ensure that the comparison is done in text mode. Otherwise, equivalent (to awk) numeric values will be seen as equal, like:



                              7:first line
                              7e0:second line
                              7.0000:third line
                              7.000000000000000000008:fourth line


                            • {print""} print an empty line (if all the tests above match).


                            • {last=$1} store the line first field in the variable last.


                            • 1 print the line contents (always).








                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered Jan 25 at 18:31









                            IsaacIsaac

                            12k11852




                            12k11852






























                                draft saved

                                draft discarded




















































                                Thanks for contributing an answer to Unix & Linux Stack Exchange!


                                • 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%2funix.stackexchange.com%2fquestions%2f496684%2fgroup-lines-according-to-first-word%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

                                Mario Kart Wii

                                The Binding of Isaac: Rebirth/Afterbirth

                                What does “Dominus providebit” mean?