Portable way to remove the first line from the pattern space (when multiple lines are present)

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











up vote
3
down vote

favorite












What's the portable way to delete the first line1 from the pattern space ?

With gnu sed I can do



s/[^n]*n//


but as far as I know this (using n in a bracket expression) is not portable.




Practical example: here, sed prints the last section of the file including the delimiter via portable code. I'd like to remove the first line from the pattern space so as to exclude the delimiter and do that in a portable manner. With gnu sed it's simple:



sed 'H;/===/h;$!d;//d;x;s/[^n]*n//' infile



1: Obviously this should be done without restarting the cycle of commands...










share|improve this question





















  • Indeed. POSIX sed only supports BREs (Basic Regular Expressions), and in BREs ”the special characters ., *, [, and `\` […] shall lose their special meaning within a bracket expression.“
    – myrdd
    2 days ago















up vote
3
down vote

favorite












What's the portable way to delete the first line1 from the pattern space ?

With gnu sed I can do



s/[^n]*n//


but as far as I know this (using n in a bracket expression) is not portable.




Practical example: here, sed prints the last section of the file including the delimiter via portable code. I'd like to remove the first line from the pattern space so as to exclude the delimiter and do that in a portable manner. With gnu sed it's simple:



sed 'H;/===/h;$!d;//d;x;s/[^n]*n//' infile



1: Obviously this should be done without restarting the cycle of commands...










share|improve this question





















  • Indeed. POSIX sed only supports BREs (Basic Regular Expressions), and in BREs ”the special characters ., *, [, and `\` […] shall lose their special meaning within a bracket expression.“
    – myrdd
    2 days ago













up vote
3
down vote

favorite









up vote
3
down vote

favorite











What's the portable way to delete the first line1 from the pattern space ?

With gnu sed I can do



s/[^n]*n//


but as far as I know this (using n in a bracket expression) is not portable.




Practical example: here, sed prints the last section of the file including the delimiter via portable code. I'd like to remove the first line from the pattern space so as to exclude the delimiter and do that in a portable manner. With gnu sed it's simple:



sed 'H;/===/h;$!d;//d;x;s/[^n]*n//' infile



1: Obviously this should be done without restarting the cycle of commands...










share|improve this question













What's the portable way to delete the first line1 from the pattern space ?

With gnu sed I can do



s/[^n]*n//


but as far as I know this (using n in a bracket expression) is not portable.




Practical example: here, sed prints the last section of the file including the delimiter via portable code. I'd like to remove the first line from the pattern space so as to exclude the delimiter and do that in a portable manner. With gnu sed it's simple:



sed 'H;/===/h;$!d;//d;x;s/[^n]*n//' infile



1: Obviously this should be done without restarting the cycle of commands...







sed






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked 2 days ago









don_crissti

47k15124154




47k15124154











  • Indeed. POSIX sed only supports BREs (Basic Regular Expressions), and in BREs ”the special characters ., *, [, and `\` […] shall lose their special meaning within a bracket expression.“
    – myrdd
    2 days ago

















  • Indeed. POSIX sed only supports BREs (Basic Regular Expressions), and in BREs ”the special characters ., *, [, and `\` […] shall lose their special meaning within a bracket expression.“
    – myrdd
    2 days ago
















Indeed. POSIX sed only supports BREs (Basic Regular Expressions), and in BREs ”the special characters ., *, [, and `\` […] shall lose their special meaning within a bracket expression.“
– myrdd
2 days ago





Indeed. POSIX sed only supports BREs (Basic Regular Expressions), and in BREs ”the special characters ., *, [, and `\` […] shall lose their special meaning within a bracket expression.“
– myrdd
2 days ago











3 Answers
3






active

oldest

votes

















up vote
2
down vote



accepted










One way to portably do this sort of a thing is as follows:



sed -e '
# ... assuming prev sed cmds made pattern space carry newline(s)
y/n_/_n/ ;# exchange newlines with an underscore
s/^[^_]*_// ;# remove up till the first underscore, ummm newline
y/n_/_n/ ;# revert the transformation
'





share|improve this answer



























    up vote
    2
    down vote













    I don't have a suitable sed to try, but you can always do interior loops testing for and removing characters until you get to the newline:



    sed 'H
    /===/h
    $!d
    //d
    x

    :rpt
    s/^n//
    t done
    s/.//
    t rpt

    :done
    '


    The second test-branch, t rpt, needs to be a t rather than a b in order to reset the internal flag that says an s has suceeded since the last read. You don't need the , they are just to show the loop better.






    share|improve this answer





























      up vote
      1
      down vote













      Silly, but working:



      sed 'h;G;s/n/&&/;s/^(.*)n(.*)n12$/2/'


      What's that? You double the whole content, then replace the first newline with two newlines. So you have the same content twice, with one additional newline after the first line. With backreferences you can now identify the different parts.



      If you don't want to use the hold buffer:



      sed 's/.*/&&/;s/n/&&/;s/^(.*)n(.*)12$/2/'


      And no, I don't like that. If there is a way to avoid it, avoid it.






      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%2f468002%2fportable-way-to-remove-the-first-line-from-the-pattern-space-when-multiple-line%23new-answer', 'question_page');

        );

        Post as a guest






























        3 Answers
        3






        active

        oldest

        votes








        3 Answers
        3






        active

        oldest

        votes









        active

        oldest

        votes






        active

        oldest

        votes








        up vote
        2
        down vote



        accepted










        One way to portably do this sort of a thing is as follows:



        sed -e '
        # ... assuming prev sed cmds made pattern space carry newline(s)
        y/n_/_n/ ;# exchange newlines with an underscore
        s/^[^_]*_// ;# remove up till the first underscore, ummm newline
        y/n_/_n/ ;# revert the transformation
        '





        share|improve this answer
























          up vote
          2
          down vote



          accepted










          One way to portably do this sort of a thing is as follows:



          sed -e '
          # ... assuming prev sed cmds made pattern space carry newline(s)
          y/n_/_n/ ;# exchange newlines with an underscore
          s/^[^_]*_// ;# remove up till the first underscore, ummm newline
          y/n_/_n/ ;# revert the transformation
          '





          share|improve this answer






















            up vote
            2
            down vote



            accepted







            up vote
            2
            down vote



            accepted






            One way to portably do this sort of a thing is as follows:



            sed -e '
            # ... assuming prev sed cmds made pattern space carry newline(s)
            y/n_/_n/ ;# exchange newlines with an underscore
            s/^[^_]*_// ;# remove up till the first underscore, ummm newline
            y/n_/_n/ ;# revert the transformation
            '





            share|improve this answer












            One way to portably do this sort of a thing is as follows:



            sed -e '
            # ... assuming prev sed cmds made pattern space carry newline(s)
            y/n_/_n/ ;# exchange newlines with an underscore
            s/^[^_]*_// ;# remove up till the first underscore, ummm newline
            y/n_/_n/ ;# revert the transformation
            '






            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered yesterday









            Rakesh Sharma

            55513




            55513






















                up vote
                2
                down vote













                I don't have a suitable sed to try, but you can always do interior loops testing for and removing characters until you get to the newline:



                sed 'H
                /===/h
                $!d
                //d
                x

                :rpt
                s/^n//
                t done
                s/.//
                t rpt

                :done
                '


                The second test-branch, t rpt, needs to be a t rather than a b in order to reset the internal flag that says an s has suceeded since the last read. You don't need the , they are just to show the loop better.






                share|improve this answer


























                  up vote
                  2
                  down vote













                  I don't have a suitable sed to try, but you can always do interior loops testing for and removing characters until you get to the newline:



                  sed 'H
                  /===/h
                  $!d
                  //d
                  x

                  :rpt
                  s/^n//
                  t done
                  s/.//
                  t rpt

                  :done
                  '


                  The second test-branch, t rpt, needs to be a t rather than a b in order to reset the internal flag that says an s has suceeded since the last read. You don't need the , they are just to show the loop better.






                  share|improve this answer
























                    up vote
                    2
                    down vote










                    up vote
                    2
                    down vote









                    I don't have a suitable sed to try, but you can always do interior loops testing for and removing characters until you get to the newline:



                    sed 'H
                    /===/h
                    $!d
                    //d
                    x

                    :rpt
                    s/^n//
                    t done
                    s/.//
                    t rpt

                    :done
                    '


                    The second test-branch, t rpt, needs to be a t rather than a b in order to reset the internal flag that says an s has suceeded since the last read. You don't need the , they are just to show the loop better.






                    share|improve this answer














                    I don't have a suitable sed to try, but you can always do interior loops testing for and removing characters until you get to the newline:



                    sed 'H
                    /===/h
                    $!d
                    //d
                    x

                    :rpt
                    s/^n//
                    t done
                    s/.//
                    t rpt

                    :done
                    '


                    The second test-branch, t rpt, needs to be a t rather than a b in order to reset the internal flag that says an s has suceeded since the last read. You don't need the , they are just to show the loop better.







                    share|improve this answer














                    share|improve this answer



                    share|improve this answer








                    edited 2 days ago

























                    answered 2 days ago









                    meuh

                    29.8k11751




                    29.8k11751




















                        up vote
                        1
                        down vote













                        Silly, but working:



                        sed 'h;G;s/n/&&/;s/^(.*)n(.*)n12$/2/'


                        What's that? You double the whole content, then replace the first newline with two newlines. So you have the same content twice, with one additional newline after the first line. With backreferences you can now identify the different parts.



                        If you don't want to use the hold buffer:



                        sed 's/.*/&&/;s/n/&&/;s/^(.*)n(.*)12$/2/'


                        And no, I don't like that. If there is a way to avoid it, avoid it.






                        share|improve this answer


























                          up vote
                          1
                          down vote













                          Silly, but working:



                          sed 'h;G;s/n/&&/;s/^(.*)n(.*)n12$/2/'


                          What's that? You double the whole content, then replace the first newline with two newlines. So you have the same content twice, with one additional newline after the first line. With backreferences you can now identify the different parts.



                          If you don't want to use the hold buffer:



                          sed 's/.*/&&/;s/n/&&/;s/^(.*)n(.*)12$/2/'


                          And no, I don't like that. If there is a way to avoid it, avoid it.






                          share|improve this answer
























                            up vote
                            1
                            down vote










                            up vote
                            1
                            down vote









                            Silly, but working:



                            sed 'h;G;s/n/&&/;s/^(.*)n(.*)n12$/2/'


                            What's that? You double the whole content, then replace the first newline with two newlines. So you have the same content twice, with one additional newline after the first line. With backreferences you can now identify the different parts.



                            If you don't want to use the hold buffer:



                            sed 's/.*/&&/;s/n/&&/;s/^(.*)n(.*)12$/2/'


                            And no, I don't like that. If there is a way to avoid it, avoid it.






                            share|improve this answer














                            Silly, but working:



                            sed 'h;G;s/n/&&/;s/^(.*)n(.*)n12$/2/'


                            What's that? You double the whole content, then replace the first newline with two newlines. So you have the same content twice, with one additional newline after the first line. With backreferences you can now identify the different parts.



                            If you don't want to use the hold buffer:



                            sed 's/.*/&&/;s/n/&&/;s/^(.*)n(.*)12$/2/'


                            And no, I don't like that. If there is a way to avoid it, avoid it.







                            share|improve this answer














                            share|improve this answer



                            share|improve this answer








                            edited 2 days ago









                            don_crissti

                            47k15124154




                            47k15124154










                            answered 2 days ago









                            Philippos

                            5,93211546




                            5,93211546



























                                 

                                draft saved


                                draft discarded















































                                 


                                draft saved


                                draft discarded














                                StackExchange.ready(
                                function ()
                                StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f468002%2fportable-way-to-remove-the-first-line-from-the-pattern-space-when-multiple-line%23new-answer', 'question_page');

                                );

                                Post as a guest













































































                                Comments

                                Popular posts from this blog

                                What does second last employer means? [closed]

                                Installing NextGIS Connect into QGIS 3?

                                One-line joke