Sort a file while grouping indented lines with their parent (multiple level)

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











up vote
2
down vote

favorite
1












All the levels should be sorted alphabetically (but must be kept with their parent)



File Example:



first
apple
orange
train
car
kiwi
third
orange
apple
plane
second
lemon


Expected Result:



first
apple
kiwi
orange
car
train
second
lemon
third
apple
plane
orange


The following command has been used but it works only if the file has only two levels into the tree.



sed '/^[^[:blank:]]/h;//!G;s/(.*)n(.*)/2x021/' infile | sort | sed 's/.*x02//'


How can I do to sort all the levels correctly?



Thanks in advance







share|improve this question

















  • 2




    please format your input content in proper way (as it actually looks). Copy and paste, then use (code sample) on selected fragment
    – RomanPerekhrest
    Jul 3 at 17:04











  • could the file have more than 3 levels?
    – RomanPerekhrest
    Jul 3 at 17:23










  • 4 levels are possible
    – nick10
    Jul 3 at 17:31










  • are there spaces before first, second (1st level) values?
    – RomanPerekhrest
    Jul 3 at 17:33











  • No Spaces before the first level values
    – nick10
    Jul 3 at 17:35














up vote
2
down vote

favorite
1












All the levels should be sorted alphabetically (but must be kept with their parent)



File Example:



first
apple
orange
train
car
kiwi
third
orange
apple
plane
second
lemon


Expected Result:



first
apple
kiwi
orange
car
train
second
lemon
third
apple
plane
orange


The following command has been used but it works only if the file has only two levels into the tree.



sed '/^[^[:blank:]]/h;//!G;s/(.*)n(.*)/2x021/' infile | sort | sed 's/.*x02//'


How can I do to sort all the levels correctly?



Thanks in advance







share|improve this question

















  • 2




    please format your input content in proper way (as it actually looks). Copy and paste, then use (code sample) on selected fragment
    – RomanPerekhrest
    Jul 3 at 17:04











  • could the file have more than 3 levels?
    – RomanPerekhrest
    Jul 3 at 17:23










  • 4 levels are possible
    – nick10
    Jul 3 at 17:31










  • are there spaces before first, second (1st level) values?
    – RomanPerekhrest
    Jul 3 at 17:33











  • No Spaces before the first level values
    – nick10
    Jul 3 at 17:35












up vote
2
down vote

favorite
1









up vote
2
down vote

favorite
1






1





All the levels should be sorted alphabetically (but must be kept with their parent)



File Example:



first
apple
orange
train
car
kiwi
third
orange
apple
plane
second
lemon


Expected Result:



first
apple
kiwi
orange
car
train
second
lemon
third
apple
plane
orange


The following command has been used but it works only if the file has only two levels into the tree.



sed '/^[^[:blank:]]/h;//!G;s/(.*)n(.*)/2x021/' infile | sort | sed 's/.*x02//'


How can I do to sort all the levels correctly?



Thanks in advance







share|improve this question













All the levels should be sorted alphabetically (but must be kept with their parent)



File Example:



first
apple
orange
train
car
kiwi
third
orange
apple
plane
second
lemon


Expected Result:



first
apple
kiwi
orange
car
train
second
lemon
third
apple
plane
orange


The following command has been used but it works only if the file has only two levels into the tree.



sed '/^[^[:blank:]]/h;//!G;s/(.*)n(.*)/2x021/' infile | sort | sed 's/.*x02//'


How can I do to sort all the levels correctly?



Thanks in advance









share|improve this question












share|improve this question




share|improve this question








edited Jul 4 at 16:25









Isaac

6,2331632




6,2331632









asked Jul 3 at 16:59









nick10

164




164







  • 2




    please format your input content in proper way (as it actually looks). Copy and paste, then use (code sample) on selected fragment
    – RomanPerekhrest
    Jul 3 at 17:04











  • could the file have more than 3 levels?
    – RomanPerekhrest
    Jul 3 at 17:23










  • 4 levels are possible
    – nick10
    Jul 3 at 17:31










  • are there spaces before first, second (1st level) values?
    – RomanPerekhrest
    Jul 3 at 17:33











  • No Spaces before the first level values
    – nick10
    Jul 3 at 17:35












  • 2




    please format your input content in proper way (as it actually looks). Copy and paste, then use (code sample) on selected fragment
    – RomanPerekhrest
    Jul 3 at 17:04











  • could the file have more than 3 levels?
    – RomanPerekhrest
    Jul 3 at 17:23










  • 4 levels are possible
    – nick10
    Jul 3 at 17:31










  • are there spaces before first, second (1st level) values?
    – RomanPerekhrest
    Jul 3 at 17:33











  • No Spaces before the first level values
    – nick10
    Jul 3 at 17:35







2




2




please format your input content in proper way (as it actually looks). Copy and paste, then use (code sample) on selected fragment
– RomanPerekhrest
Jul 3 at 17:04





please format your input content in proper way (as it actually looks). Copy and paste, then use (code sample) on selected fragment
– RomanPerekhrest
Jul 3 at 17:04













could the file have more than 3 levels?
– RomanPerekhrest
Jul 3 at 17:23




could the file have more than 3 levels?
– RomanPerekhrest
Jul 3 at 17:23












4 levels are possible
– nick10
Jul 3 at 17:31




4 levels are possible
– nick10
Jul 3 at 17:31












are there spaces before first, second (1st level) values?
– RomanPerekhrest
Jul 3 at 17:33





are there spaces before first, second (1st level) values?
– RomanPerekhrest
Jul 3 at 17:33













No Spaces before the first level values
– nick10
Jul 3 at 17:35




No Spaces before the first level values
– nick10
Jul 3 at 17:35










4 Answers
4






active

oldest

votes

















up vote
1
down vote



accepted










Extended Python solution:



Sample infile contents (4 levels):



first
apple
orange
train
car
truck
automobile
kiwi
third
orange
apple
plane
second
lemon



sort_hierarchy.py script:



#!/usr/bin/env python
# -*- coding: utf-8 -*-

import sys
import re

with open(sys.argv[1], 'rt') as f:
pat = re.compile(r'^s+')
paths =

for line in f:
offset = pat.match(line)
item = line.strip()

if not offset:
offset = 0
paths.append(item)
else:
offset = offset.span()[1]
if offset > prev_offset:
paths.append(paths[-1] + '.' + item)
else:
cut_pos = -prev_offset//offset
paths.append('.'.join(paths[-1].split('.')[:cut_pos]) + '.' + item)

prev_offset = offset

paths.sort()
sub_pat = re.compile(r'[^.]+.')
for i in paths:
print(sub_pat.sub(' ' * 4, i))



Usage:



python sort_hierarchy.py path/to/infile


The output:



first
apple
kiwi
orange
car
automobile
truck
train
second
lemon
third
apple
plane
orange





share|improve this answer





















  • @nick10, learn about What should I do when someone answers my question?
    – RomanPerekhrest
    Jul 4 at 10:50


















up vote
0
down vote













Awk solution:



Sample infile contents (4 levels):



first
apple
orange
train
car
truck
automobile
kiwi
third
orange
apple
plane
second
lemon



awk '
offset = gsub(/ /, "");
if (offset == 0) items[NR] = $1
else if (offset > prev_ofst) items[NR] = items[NR-1] "." $1
else
prev_item = items[NR-1];
gsub("(\.[^.]+)" int(prev_ofst / offset) "$", "", prev_item);
items[NR] = prev_item "." $1

prev_ofst = offset;

END
asort(items);
for (i = 1; i <= NR; i++)
gsub(/[^.]+./, " ", items[i]);
print items[i]

' infile



The output:



first
apple
kiwi
orange
car
automobile
truck
train
second
lemon
third
apple
plane
orange





share|improve this answer




























    up vote
    0
    down vote













    works for any depth



    #!/usr/bin/python3
    lines = open('test_file').read().splitlines()

    def yield_sorted_lines(lines):
    sorter =
    for l in lines:
    fields = l.split('t')
    n = len(fields)
    sorter = sorter[:n-1] + fields[n-1:]
    yield sorter, l


    prefixed_lines = yield_sorted_lines(lines)
    sorted_lines = sorted(prefixed_lines, key=lambda x: x[0])
    for x, y in sorted_lines:
    print(y)


    Or an pipeline



    awk -F'\t' 'a[NF]=$NF; for (i=1; i<=NF; ++i) printf "%s%s", a[i], i==NF? "n": "t"' file|
    sort | awk -F'\t' -vOFS='t' 'for (i=1; i<NF; ++i) $i=""; print'





    share|improve this answer






























      up vote
      0
      down vote













      sed '/^ /H;$!d;x;1d;s/n/x7/g' | sort | tr \a \n


      The /continuation/H;$!d;x;1d (or /firstline/!etc) is a slurp, it falls through only when it's got a complete line gaggle in the buffer.



      If you might get a single-line gaggle at the end, add $p;x;/n/d to do the double-pump needed for that.






      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: 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%2f453267%2fsort-a-file-while-grouping-indented-lines-with-their-parent-multiple-level%23new-answer', 'question_page');

        );

        Post as a guest






























        4 Answers
        4






        active

        oldest

        votes








        4 Answers
        4






        active

        oldest

        votes









        active

        oldest

        votes






        active

        oldest

        votes








        up vote
        1
        down vote



        accepted










        Extended Python solution:



        Sample infile contents (4 levels):



        first
        apple
        orange
        train
        car
        truck
        automobile
        kiwi
        third
        orange
        apple
        plane
        second
        lemon



        sort_hierarchy.py script:



        #!/usr/bin/env python
        # -*- coding: utf-8 -*-

        import sys
        import re

        with open(sys.argv[1], 'rt') as f:
        pat = re.compile(r'^s+')
        paths =

        for line in f:
        offset = pat.match(line)
        item = line.strip()

        if not offset:
        offset = 0
        paths.append(item)
        else:
        offset = offset.span()[1]
        if offset > prev_offset:
        paths.append(paths[-1] + '.' + item)
        else:
        cut_pos = -prev_offset//offset
        paths.append('.'.join(paths[-1].split('.')[:cut_pos]) + '.' + item)

        prev_offset = offset

        paths.sort()
        sub_pat = re.compile(r'[^.]+.')
        for i in paths:
        print(sub_pat.sub(' ' * 4, i))



        Usage:



        python sort_hierarchy.py path/to/infile


        The output:



        first
        apple
        kiwi
        orange
        car
        automobile
        truck
        train
        second
        lemon
        third
        apple
        plane
        orange





        share|improve this answer





















        • @nick10, learn about What should I do when someone answers my question?
          – RomanPerekhrest
          Jul 4 at 10:50















        up vote
        1
        down vote



        accepted










        Extended Python solution:



        Sample infile contents (4 levels):



        first
        apple
        orange
        train
        car
        truck
        automobile
        kiwi
        third
        orange
        apple
        plane
        second
        lemon



        sort_hierarchy.py script:



        #!/usr/bin/env python
        # -*- coding: utf-8 -*-

        import sys
        import re

        with open(sys.argv[1], 'rt') as f:
        pat = re.compile(r'^s+')
        paths =

        for line in f:
        offset = pat.match(line)
        item = line.strip()

        if not offset:
        offset = 0
        paths.append(item)
        else:
        offset = offset.span()[1]
        if offset > prev_offset:
        paths.append(paths[-1] + '.' + item)
        else:
        cut_pos = -prev_offset//offset
        paths.append('.'.join(paths[-1].split('.')[:cut_pos]) + '.' + item)

        prev_offset = offset

        paths.sort()
        sub_pat = re.compile(r'[^.]+.')
        for i in paths:
        print(sub_pat.sub(' ' * 4, i))



        Usage:



        python sort_hierarchy.py path/to/infile


        The output:



        first
        apple
        kiwi
        orange
        car
        automobile
        truck
        train
        second
        lemon
        third
        apple
        plane
        orange





        share|improve this answer





















        • @nick10, learn about What should I do when someone answers my question?
          – RomanPerekhrest
          Jul 4 at 10:50













        up vote
        1
        down vote



        accepted







        up vote
        1
        down vote



        accepted






        Extended Python solution:



        Sample infile contents (4 levels):



        first
        apple
        orange
        train
        car
        truck
        automobile
        kiwi
        third
        orange
        apple
        plane
        second
        lemon



        sort_hierarchy.py script:



        #!/usr/bin/env python
        # -*- coding: utf-8 -*-

        import sys
        import re

        with open(sys.argv[1], 'rt') as f:
        pat = re.compile(r'^s+')
        paths =

        for line in f:
        offset = pat.match(line)
        item = line.strip()

        if not offset:
        offset = 0
        paths.append(item)
        else:
        offset = offset.span()[1]
        if offset > prev_offset:
        paths.append(paths[-1] + '.' + item)
        else:
        cut_pos = -prev_offset//offset
        paths.append('.'.join(paths[-1].split('.')[:cut_pos]) + '.' + item)

        prev_offset = offset

        paths.sort()
        sub_pat = re.compile(r'[^.]+.')
        for i in paths:
        print(sub_pat.sub(' ' * 4, i))



        Usage:



        python sort_hierarchy.py path/to/infile


        The output:



        first
        apple
        kiwi
        orange
        car
        automobile
        truck
        train
        second
        lemon
        third
        apple
        plane
        orange





        share|improve this answer













        Extended Python solution:



        Sample infile contents (4 levels):



        first
        apple
        orange
        train
        car
        truck
        automobile
        kiwi
        third
        orange
        apple
        plane
        second
        lemon



        sort_hierarchy.py script:



        #!/usr/bin/env python
        # -*- coding: utf-8 -*-

        import sys
        import re

        with open(sys.argv[1], 'rt') as f:
        pat = re.compile(r'^s+')
        paths =

        for line in f:
        offset = pat.match(line)
        item = line.strip()

        if not offset:
        offset = 0
        paths.append(item)
        else:
        offset = offset.span()[1]
        if offset > prev_offset:
        paths.append(paths[-1] + '.' + item)
        else:
        cut_pos = -prev_offset//offset
        paths.append('.'.join(paths[-1].split('.')[:cut_pos]) + '.' + item)

        prev_offset = offset

        paths.sort()
        sub_pat = re.compile(r'[^.]+.')
        for i in paths:
        print(sub_pat.sub(' ' * 4, i))



        Usage:



        python sort_hierarchy.py path/to/infile


        The output:



        first
        apple
        kiwi
        orange
        car
        automobile
        truck
        train
        second
        lemon
        third
        apple
        plane
        orange






        share|improve this answer













        share|improve this answer



        share|improve this answer











        answered Jul 3 at 22:00









        RomanPerekhrest

        22.4k12144




        22.4k12144











        • @nick10, learn about What should I do when someone answers my question?
          – RomanPerekhrest
          Jul 4 at 10:50

















        • @nick10, learn about What should I do when someone answers my question?
          – RomanPerekhrest
          Jul 4 at 10:50
















        @nick10, learn about What should I do when someone answers my question?
        – RomanPerekhrest
        Jul 4 at 10:50





        @nick10, learn about What should I do when someone answers my question?
        – RomanPerekhrest
        Jul 4 at 10:50













        up vote
        0
        down vote













        Awk solution:



        Sample infile contents (4 levels):



        first
        apple
        orange
        train
        car
        truck
        automobile
        kiwi
        third
        orange
        apple
        plane
        second
        lemon



        awk '
        offset = gsub(/ /, "");
        if (offset == 0) items[NR] = $1
        else if (offset > prev_ofst) items[NR] = items[NR-1] "." $1
        else
        prev_item = items[NR-1];
        gsub("(\.[^.]+)" int(prev_ofst / offset) "$", "", prev_item);
        items[NR] = prev_item "." $1

        prev_ofst = offset;

        END
        asort(items);
        for (i = 1; i <= NR; i++)
        gsub(/[^.]+./, " ", items[i]);
        print items[i]

        ' infile



        The output:



        first
        apple
        kiwi
        orange
        car
        automobile
        truck
        train
        second
        lemon
        third
        apple
        plane
        orange





        share|improve this answer

























          up vote
          0
          down vote













          Awk solution:



          Sample infile contents (4 levels):



          first
          apple
          orange
          train
          car
          truck
          automobile
          kiwi
          third
          orange
          apple
          plane
          second
          lemon



          awk '
          offset = gsub(/ /, "");
          if (offset == 0) items[NR] = $1
          else if (offset > prev_ofst) items[NR] = items[NR-1] "." $1
          else
          prev_item = items[NR-1];
          gsub("(\.[^.]+)" int(prev_ofst / offset) "$", "", prev_item);
          items[NR] = prev_item "." $1

          prev_ofst = offset;

          END
          asort(items);
          for (i = 1; i <= NR; i++)
          gsub(/[^.]+./, " ", items[i]);
          print items[i]

          ' infile



          The output:



          first
          apple
          kiwi
          orange
          car
          automobile
          truck
          train
          second
          lemon
          third
          apple
          plane
          orange





          share|improve this answer























            up vote
            0
            down vote










            up vote
            0
            down vote









            Awk solution:



            Sample infile contents (4 levels):



            first
            apple
            orange
            train
            car
            truck
            automobile
            kiwi
            third
            orange
            apple
            plane
            second
            lemon



            awk '
            offset = gsub(/ /, "");
            if (offset == 0) items[NR] = $1
            else if (offset > prev_ofst) items[NR] = items[NR-1] "." $1
            else
            prev_item = items[NR-1];
            gsub("(\.[^.]+)" int(prev_ofst / offset) "$", "", prev_item);
            items[NR] = prev_item "." $1

            prev_ofst = offset;

            END
            asort(items);
            for (i = 1; i <= NR; i++)
            gsub(/[^.]+./, " ", items[i]);
            print items[i]

            ' infile



            The output:



            first
            apple
            kiwi
            orange
            car
            automobile
            truck
            train
            second
            lemon
            third
            apple
            plane
            orange





            share|improve this answer













            Awk solution:



            Sample infile contents (4 levels):



            first
            apple
            orange
            train
            car
            truck
            automobile
            kiwi
            third
            orange
            apple
            plane
            second
            lemon



            awk '
            offset = gsub(/ /, "");
            if (offset == 0) items[NR] = $1
            else if (offset > prev_ofst) items[NR] = items[NR-1] "." $1
            else
            prev_item = items[NR-1];
            gsub("(\.[^.]+)" int(prev_ofst / offset) "$", "", prev_item);
            items[NR] = prev_item "." $1

            prev_ofst = offset;

            END
            asort(items);
            for (i = 1; i <= NR; i++)
            gsub(/[^.]+./, " ", items[i]);
            print items[i]

            ' infile



            The output:



            first
            apple
            kiwi
            orange
            car
            automobile
            truck
            train
            second
            lemon
            third
            apple
            plane
            orange






            share|improve this answer













            share|improve this answer



            share|improve this answer











            answered Jul 3 at 22:06









            RomanPerekhrest

            22.4k12144




            22.4k12144




















                up vote
                0
                down vote













                works for any depth



                #!/usr/bin/python3
                lines = open('test_file').read().splitlines()

                def yield_sorted_lines(lines):
                sorter =
                for l in lines:
                fields = l.split('t')
                n = len(fields)
                sorter = sorter[:n-1] + fields[n-1:]
                yield sorter, l


                prefixed_lines = yield_sorted_lines(lines)
                sorted_lines = sorted(prefixed_lines, key=lambda x: x[0])
                for x, y in sorted_lines:
                print(y)


                Or an pipeline



                awk -F'\t' 'a[NF]=$NF; for (i=1; i<=NF; ++i) printf "%s%s", a[i], i==NF? "n": "t"' file|
                sort | awk -F'\t' -vOFS='t' 'for (i=1; i<NF; ++i) $i=""; print'





                share|improve this answer



























                  up vote
                  0
                  down vote













                  works for any depth



                  #!/usr/bin/python3
                  lines = open('test_file').read().splitlines()

                  def yield_sorted_lines(lines):
                  sorter =
                  for l in lines:
                  fields = l.split('t')
                  n = len(fields)
                  sorter = sorter[:n-1] + fields[n-1:]
                  yield sorter, l


                  prefixed_lines = yield_sorted_lines(lines)
                  sorted_lines = sorted(prefixed_lines, key=lambda x: x[0])
                  for x, y in sorted_lines:
                  print(y)


                  Or an pipeline



                  awk -F'\t' 'a[NF]=$NF; for (i=1; i<=NF; ++i) printf "%s%s", a[i], i==NF? "n": "t"' file|
                  sort | awk -F'\t' -vOFS='t' 'for (i=1; i<NF; ++i) $i=""; print'





                  share|improve this answer

























                    up vote
                    0
                    down vote










                    up vote
                    0
                    down vote









                    works for any depth



                    #!/usr/bin/python3
                    lines = open('test_file').read().splitlines()

                    def yield_sorted_lines(lines):
                    sorter =
                    for l in lines:
                    fields = l.split('t')
                    n = len(fields)
                    sorter = sorter[:n-1] + fields[n-1:]
                    yield sorter, l


                    prefixed_lines = yield_sorted_lines(lines)
                    sorted_lines = sorted(prefixed_lines, key=lambda x: x[0])
                    for x, y in sorted_lines:
                    print(y)


                    Or an pipeline



                    awk -F'\t' 'a[NF]=$NF; for (i=1; i<=NF; ++i) printf "%s%s", a[i], i==NF? "n": "t"' file|
                    sort | awk -F'\t' -vOFS='t' 'for (i=1; i<NF; ++i) $i=""; print'





                    share|improve this answer















                    works for any depth



                    #!/usr/bin/python3
                    lines = open('test_file').read().splitlines()

                    def yield_sorted_lines(lines):
                    sorter =
                    for l in lines:
                    fields = l.split('t')
                    n = len(fields)
                    sorter = sorter[:n-1] + fields[n-1:]
                    yield sorter, l


                    prefixed_lines = yield_sorted_lines(lines)
                    sorted_lines = sorted(prefixed_lines, key=lambda x: x[0])
                    for x, y in sorted_lines:
                    print(y)


                    Or an pipeline



                    awk -F'\t' 'a[NF]=$NF; for (i=1; i<=NF; ++i) printf "%s%s", a[i], i==NF? "n": "t"' file|
                    sort | awk -F'\t' -vOFS='t' 'for (i=1; i<NF; ++i) $i=""; print'






                    share|improve this answer















                    share|improve this answer



                    share|improve this answer








                    edited Jul 4 at 15:36


























                    answered Jul 4 at 11:40









                    iruvar

                    11.4k62959




                    11.4k62959




















                        up vote
                        0
                        down vote













                        sed '/^ /H;$!d;x;1d;s/n/x7/g' | sort | tr \a \n


                        The /continuation/H;$!d;x;1d (or /firstline/!etc) is a slurp, it falls through only when it's got a complete line gaggle in the buffer.



                        If you might get a single-line gaggle at the end, add $p;x;/n/d to do the double-pump needed for that.






                        share|improve this answer

























                          up vote
                          0
                          down vote













                          sed '/^ /H;$!d;x;1d;s/n/x7/g' | sort | tr \a \n


                          The /continuation/H;$!d;x;1d (or /firstline/!etc) is a slurp, it falls through only when it's got a complete line gaggle in the buffer.



                          If you might get a single-line gaggle at the end, add $p;x;/n/d to do the double-pump needed for that.






                          share|improve this answer























                            up vote
                            0
                            down vote










                            up vote
                            0
                            down vote









                            sed '/^ /H;$!d;x;1d;s/n/x7/g' | sort | tr \a \n


                            The /continuation/H;$!d;x;1d (or /firstline/!etc) is a slurp, it falls through only when it's got a complete line gaggle in the buffer.



                            If you might get a single-line gaggle at the end, add $p;x;/n/d to do the double-pump needed for that.






                            share|improve this answer













                            sed '/^ /H;$!d;x;1d;s/n/x7/g' | sort | tr \a \n


                            The /continuation/H;$!d;x;1d (or /firstline/!etc) is a slurp, it falls through only when it's got a complete line gaggle in the buffer.



                            If you might get a single-line gaggle at the end, add $p;x;/n/d to do the double-pump needed for that.







                            share|improve this answer













                            share|improve this answer



                            share|improve this answer











                            answered Jul 4 at 19:40









                            jthill

                            2,283715




                            2,283715






















                                 

                                draft saved


                                draft discarded


























                                 


                                draft saved


                                draft discarded














                                StackExchange.ready(
                                function ()
                                StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f453267%2fsort-a-file-while-grouping-indented-lines-with-their-parent-multiple-level%23new-answer', 'question_page');

                                );

                                Post as a guest













































































                                Popular posts from this blog

                                How to check contact read email or not when send email to Individual?

                                Bahrain

                                Postfix configuration issue with fips on centos 7; mailgun relay