sed search and show matching pattern, removing the rest

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





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








0















I am trying to clean a list containing packages. I wish to remove the package revisions, leaving the package names only.



I see every package has naming convention like this: package-name-majorver-minorver , etc: openssl-1.0.1e-57.0.5.el6 . I want openssl only, not -1.0.1e-57.0.5.el6



So far I have this sed 's/^.*-//', but this doesn't work obviously.










share|improve this question






















  • watch out for package names with dashes! If you're gathering the list with RPM, consider using the --queryformat to get the %NAME separately.

    – Jeff Schaller
    Mar 13 at 11:05

















0















I am trying to clean a list containing packages. I wish to remove the package revisions, leaving the package names only.



I see every package has naming convention like this: package-name-majorver-minorver , etc: openssl-1.0.1e-57.0.5.el6 . I want openssl only, not -1.0.1e-57.0.5.el6



So far I have this sed 's/^.*-//', but this doesn't work obviously.










share|improve this question






















  • watch out for package names with dashes! If you're gathering the list with RPM, consider using the --queryformat to get the %NAME separately.

    – Jeff Schaller
    Mar 13 at 11:05













0












0








0








I am trying to clean a list containing packages. I wish to remove the package revisions, leaving the package names only.



I see every package has naming convention like this: package-name-majorver-minorver , etc: openssl-1.0.1e-57.0.5.el6 . I want openssl only, not -1.0.1e-57.0.5.el6



So far I have this sed 's/^.*-//', but this doesn't work obviously.










share|improve this question














I am trying to clean a list containing packages. I wish to remove the package revisions, leaving the package names only.



I see every package has naming convention like this: package-name-majorver-minorver , etc: openssl-1.0.1e-57.0.5.el6 . I want openssl only, not -1.0.1e-57.0.5.el6



So far I have this sed 's/^.*-//', but this doesn't work obviously.







sed package-management rpm






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Mar 13 at 9:49









user121392user121392

85




85












  • watch out for package names with dashes! If you're gathering the list with RPM, consider using the --queryformat to get the %NAME separately.

    – Jeff Schaller
    Mar 13 at 11:05

















  • watch out for package names with dashes! If you're gathering the list with RPM, consider using the --queryformat to get the %NAME separately.

    – Jeff Schaller
    Mar 13 at 11:05
















watch out for package names with dashes! If you're gathering the list with RPM, consider using the --queryformat to get the %NAME separately.

– Jeff Schaller
Mar 13 at 11:05





watch out for package names with dashes! If you're gathering the list with RPM, consider using the --queryformat to get the %NAME separately.

– Jeff Schaller
Mar 13 at 11:05










3 Answers
3






active

oldest

votes


















1














If the package name always has two version number parts appended, separated by hyphens, and if the version parts never contain a hyphen, you can use this command



sed 's/-[^-]*-[^-]*$//'


This will remove two hyphens, each followed by 0 or more non-hyphen characters, at the end of the line ($). It would also change e.g. foo-bar-baz-- to foo-bar-baz.






share|improve this answer






























    0














    If the package name doesn't contain -, this should work:



    sed -r 's/([^-]+).*/1/' 





    share|improve this answer























    • unfortunately, it does, and the number of - 's are inconsistent too, eg: tmux and python-pip, the only consistent ones are major ver and minor ver appended to back separated by -

      – user121392
      Mar 13 at 10:10


















    0














    Well I know it's not the best solution, but I found this https://stackoverflow.com/a/51153277/5227747, so the solution was



    cat file | sed 's/ // ; s/-/t/' | rev | cut -f 3- | rev | sed 's/t/-/'


    it removes the last two '-'-separated columns without fuss, if anyone knows how to do it better I would love to know.






    share|improve this answer























    • Unneccessary cat

      – Weijun Zhou
      Mar 13 at 10:57











    • I have to read from a file. I will check sed to improve the command

      – user121392
      Mar 13 at 11:49











    • You should almost always do sed ... file instead of cat file | sed ...

      – Weijun Zhou
      Mar 13 at 22:19











    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%2f506048%2fsed-search-and-show-matching-pattern-removing-the-rest%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    3 Answers
    3






    active

    oldest

    votes








    3 Answers
    3






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    1














    If the package name always has two version number parts appended, separated by hyphens, and if the version parts never contain a hyphen, you can use this command



    sed 's/-[^-]*-[^-]*$//'


    This will remove two hyphens, each followed by 0 or more non-hyphen characters, at the end of the line ($). It would also change e.g. foo-bar-baz-- to foo-bar-baz.






    share|improve this answer



























      1














      If the package name always has two version number parts appended, separated by hyphens, and if the version parts never contain a hyphen, you can use this command



      sed 's/-[^-]*-[^-]*$//'


      This will remove two hyphens, each followed by 0 or more non-hyphen characters, at the end of the line ($). It would also change e.g. foo-bar-baz-- to foo-bar-baz.






      share|improve this answer

























        1












        1








        1







        If the package name always has two version number parts appended, separated by hyphens, and if the version parts never contain a hyphen, you can use this command



        sed 's/-[^-]*-[^-]*$//'


        This will remove two hyphens, each followed by 0 or more non-hyphen characters, at the end of the line ($). It would also change e.g. foo-bar-baz-- to foo-bar-baz.






        share|improve this answer













        If the package name always has two version number parts appended, separated by hyphens, and if the version parts never contain a hyphen, you can use this command



        sed 's/-[^-]*-[^-]*$//'


        This will remove two hyphens, each followed by 0 or more non-hyphen characters, at the end of the line ($). It would also change e.g. foo-bar-baz-- to foo-bar-baz.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Mar 13 at 10:45









        BodoBodo

        2,271618




        2,271618























            0














            If the package name doesn't contain -, this should work:



            sed -r 's/([^-]+).*/1/' 





            share|improve this answer























            • unfortunately, it does, and the number of - 's are inconsistent too, eg: tmux and python-pip, the only consistent ones are major ver and minor ver appended to back separated by -

              – user121392
              Mar 13 at 10:10















            0














            If the package name doesn't contain -, this should work:



            sed -r 's/([^-]+).*/1/' 





            share|improve this answer























            • unfortunately, it does, and the number of - 's are inconsistent too, eg: tmux and python-pip, the only consistent ones are major ver and minor ver appended to back separated by -

              – user121392
              Mar 13 at 10:10













            0












            0








            0







            If the package name doesn't contain -, this should work:



            sed -r 's/([^-]+).*/1/' 





            share|improve this answer













            If the package name doesn't contain -, this should work:



            sed -r 's/([^-]+).*/1/' 






            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Mar 13 at 10:06









            CharlesCharles

            335110




            335110












            • unfortunately, it does, and the number of - 's are inconsistent too, eg: tmux and python-pip, the only consistent ones are major ver and minor ver appended to back separated by -

              – user121392
              Mar 13 at 10:10

















            • unfortunately, it does, and the number of - 's are inconsistent too, eg: tmux and python-pip, the only consistent ones are major ver and minor ver appended to back separated by -

              – user121392
              Mar 13 at 10:10
















            unfortunately, it does, and the number of - 's are inconsistent too, eg: tmux and python-pip, the only consistent ones are major ver and minor ver appended to back separated by -

            – user121392
            Mar 13 at 10:10





            unfortunately, it does, and the number of - 's are inconsistent too, eg: tmux and python-pip, the only consistent ones are major ver and minor ver appended to back separated by -

            – user121392
            Mar 13 at 10:10











            0














            Well I know it's not the best solution, but I found this https://stackoverflow.com/a/51153277/5227747, so the solution was



            cat file | sed 's/ // ; s/-/t/' | rev | cut -f 3- | rev | sed 's/t/-/'


            it removes the last two '-'-separated columns without fuss, if anyone knows how to do it better I would love to know.






            share|improve this answer























            • Unneccessary cat

              – Weijun Zhou
              Mar 13 at 10:57











            • I have to read from a file. I will check sed to improve the command

              – user121392
              Mar 13 at 11:49











            • You should almost always do sed ... file instead of cat file | sed ...

              – Weijun Zhou
              Mar 13 at 22:19















            0














            Well I know it's not the best solution, but I found this https://stackoverflow.com/a/51153277/5227747, so the solution was



            cat file | sed 's/ // ; s/-/t/' | rev | cut -f 3- | rev | sed 's/t/-/'


            it removes the last two '-'-separated columns without fuss, if anyone knows how to do it better I would love to know.






            share|improve this answer























            • Unneccessary cat

              – Weijun Zhou
              Mar 13 at 10:57











            • I have to read from a file. I will check sed to improve the command

              – user121392
              Mar 13 at 11:49











            • You should almost always do sed ... file instead of cat file | sed ...

              – Weijun Zhou
              Mar 13 at 22:19













            0












            0








            0







            Well I know it's not the best solution, but I found this https://stackoverflow.com/a/51153277/5227747, so the solution was



            cat file | sed 's/ // ; s/-/t/' | rev | cut -f 3- | rev | sed 's/t/-/'


            it removes the last two '-'-separated columns without fuss, if anyone knows how to do it better I would love to know.






            share|improve this answer













            Well I know it's not the best solution, but I found this https://stackoverflow.com/a/51153277/5227747, so the solution was



            cat file | sed 's/ // ; s/-/t/' | rev | cut -f 3- | rev | sed 's/t/-/'


            it removes the last two '-'-separated columns without fuss, if anyone knows how to do it better I would love to know.







            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Mar 13 at 10:33









            user121392user121392

            85




            85












            • Unneccessary cat

              – Weijun Zhou
              Mar 13 at 10:57











            • I have to read from a file. I will check sed to improve the command

              – user121392
              Mar 13 at 11:49











            • You should almost always do sed ... file instead of cat file | sed ...

              – Weijun Zhou
              Mar 13 at 22:19

















            • Unneccessary cat

              – Weijun Zhou
              Mar 13 at 10:57











            • I have to read from a file. I will check sed to improve the command

              – user121392
              Mar 13 at 11:49











            • You should almost always do sed ... file instead of cat file | sed ...

              – Weijun Zhou
              Mar 13 at 22:19
















            Unneccessary cat

            – Weijun Zhou
            Mar 13 at 10:57





            Unneccessary cat

            – Weijun Zhou
            Mar 13 at 10:57













            I have to read from a file. I will check sed to improve the command

            – user121392
            Mar 13 at 11:49





            I have to read from a file. I will check sed to improve the command

            – user121392
            Mar 13 at 11:49













            You should almost always do sed ... file instead of cat file | sed ...

            – Weijun Zhou
            Mar 13 at 22:19





            You should almost always do sed ... file instead of cat file | sed ...

            – Weijun Zhou
            Mar 13 at 22:19

















            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%2f506048%2fsed-search-and-show-matching-pattern-removing-the-rest%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

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

            Bahrain

            Postfix configuration issue with fips on centos 7; mailgun relay