Busybox filter string list












0














Consider I have a list like this:



list='item1.service item2.service item3.target item4.service'


I need to filter this list to get something as:



item1 item2 item4


So, there are two things to notice:




  1. I need keep only the .service items.

  2. I need only the "base" names, without the .service suffix.


And one more important information: I am running on busybox, where tools are often crippled (e.g.: my grep has no support for Perl regexes).



I have been struggling against combinations of sed and grep and the best I could get is:



$ echo $list | grep -io '[a-z0-9-_@]*.service' | sed 's/.service//'
item1
item2
item4


but it needs to perform essentially the same match twice for each input, what doesn't look very efficient.
Could anyone suggest any better solution, please?



Thanks in advance.










share|improve this question






















  • What does this have to do with Busybox?
    – JakeGould
    Dec 11 at 16:38












  • sed does all the job echo "$list"|sed -r 's/[a-z0-9@_-]+.target//;s/ +/ /g;s/ /n/g'. This works in BusyBox 1.2.1, very old.
    – Paulo
    Dec 11 at 23:27












  • Thanks @Paulo for your response. Unfortunatelly, sed doesn't remove the suffix and the generate output is item1.service instead of simply item1 as I need.
    – j4x
    Dec 12 at 8:03






  • 1




    @j4x The sed you posted has the command to cut the suffix, just append it to sed script sed -r 's/[a-z0-9@_-]+.target//;s/.service//g;s/ +/ /g;s/ /n/g'. g flag is needed to replace all matches, without it sed will replace only the first match.
    – Paulo
    Dec 12 at 11:52












  • Good. IT is devilish to understand but works! @Paulo, please post an answer so I can mark it. Thanks!
    – j4x
    Dec 12 at 12:13
















0














Consider I have a list like this:



list='item1.service item2.service item3.target item4.service'


I need to filter this list to get something as:



item1 item2 item4


So, there are two things to notice:




  1. I need keep only the .service items.

  2. I need only the "base" names, without the .service suffix.


And one more important information: I am running on busybox, where tools are often crippled (e.g.: my grep has no support for Perl regexes).



I have been struggling against combinations of sed and grep and the best I could get is:



$ echo $list | grep -io '[a-z0-9-_@]*.service' | sed 's/.service//'
item1
item2
item4


but it needs to perform essentially the same match twice for each input, what doesn't look very efficient.
Could anyone suggest any better solution, please?



Thanks in advance.










share|improve this question






















  • What does this have to do with Busybox?
    – JakeGould
    Dec 11 at 16:38












  • sed does all the job echo "$list"|sed -r 's/[a-z0-9@_-]+.target//;s/ +/ /g;s/ /n/g'. This works in BusyBox 1.2.1, very old.
    – Paulo
    Dec 11 at 23:27












  • Thanks @Paulo for your response. Unfortunatelly, sed doesn't remove the suffix and the generate output is item1.service instead of simply item1 as I need.
    – j4x
    Dec 12 at 8:03






  • 1




    @j4x The sed you posted has the command to cut the suffix, just append it to sed script sed -r 's/[a-z0-9@_-]+.target//;s/.service//g;s/ +/ /g;s/ /n/g'. g flag is needed to replace all matches, without it sed will replace only the first match.
    – Paulo
    Dec 12 at 11:52












  • Good. IT is devilish to understand but works! @Paulo, please post an answer so I can mark it. Thanks!
    – j4x
    Dec 12 at 12:13














0












0








0







Consider I have a list like this:



list='item1.service item2.service item3.target item4.service'


I need to filter this list to get something as:



item1 item2 item4


So, there are two things to notice:




  1. I need keep only the .service items.

  2. I need only the "base" names, without the .service suffix.


And one more important information: I am running on busybox, where tools are often crippled (e.g.: my grep has no support for Perl regexes).



I have been struggling against combinations of sed and grep and the best I could get is:



$ echo $list | grep -io '[a-z0-9-_@]*.service' | sed 's/.service//'
item1
item2
item4


but it needs to perform essentially the same match twice for each input, what doesn't look very efficient.
Could anyone suggest any better solution, please?



Thanks in advance.










share|improve this question













Consider I have a list like this:



list='item1.service item2.service item3.target item4.service'


I need to filter this list to get something as:



item1 item2 item4


So, there are two things to notice:




  1. I need keep only the .service items.

  2. I need only the "base" names, without the .service suffix.


And one more important information: I am running on busybox, where tools are often crippled (e.g.: my grep has no support for Perl regexes).



I have been struggling against combinations of sed and grep and the best I could get is:



$ echo $list | grep -io '[a-z0-9-_@]*.service' | sed 's/.service//'
item1
item2
item4


but it needs to perform essentially the same match twice for each input, what doesn't look very efficient.
Could anyone suggest any better solution, please?



Thanks in advance.







regex string-manipulation busybox






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Dec 11 at 16:06









j4x

1054




1054












  • What does this have to do with Busybox?
    – JakeGould
    Dec 11 at 16:38












  • sed does all the job echo "$list"|sed -r 's/[a-z0-9@_-]+.target//;s/ +/ /g;s/ /n/g'. This works in BusyBox 1.2.1, very old.
    – Paulo
    Dec 11 at 23:27












  • Thanks @Paulo for your response. Unfortunatelly, sed doesn't remove the suffix and the generate output is item1.service instead of simply item1 as I need.
    – j4x
    Dec 12 at 8:03






  • 1




    @j4x The sed you posted has the command to cut the suffix, just append it to sed script sed -r 's/[a-z0-9@_-]+.target//;s/.service//g;s/ +/ /g;s/ /n/g'. g flag is needed to replace all matches, without it sed will replace only the first match.
    – Paulo
    Dec 12 at 11:52












  • Good. IT is devilish to understand but works! @Paulo, please post an answer so I can mark it. Thanks!
    – j4x
    Dec 12 at 12:13


















  • What does this have to do with Busybox?
    – JakeGould
    Dec 11 at 16:38












  • sed does all the job echo "$list"|sed -r 's/[a-z0-9@_-]+.target//;s/ +/ /g;s/ /n/g'. This works in BusyBox 1.2.1, very old.
    – Paulo
    Dec 11 at 23:27












  • Thanks @Paulo for your response. Unfortunatelly, sed doesn't remove the suffix and the generate output is item1.service instead of simply item1 as I need.
    – j4x
    Dec 12 at 8:03






  • 1




    @j4x The sed you posted has the command to cut the suffix, just append it to sed script sed -r 's/[a-z0-9@_-]+.target//;s/.service//g;s/ +/ /g;s/ /n/g'. g flag is needed to replace all matches, without it sed will replace only the first match.
    – Paulo
    Dec 12 at 11:52












  • Good. IT is devilish to understand but works! @Paulo, please post an answer so I can mark it. Thanks!
    – j4x
    Dec 12 at 12:13
















What does this have to do with Busybox?
– JakeGould
Dec 11 at 16:38






What does this have to do with Busybox?
– JakeGould
Dec 11 at 16:38














sed does all the job echo "$list"|sed -r 's/[a-z0-9@_-]+.target//;s/ +/ /g;s/ /n/g'. This works in BusyBox 1.2.1, very old.
– Paulo
Dec 11 at 23:27






sed does all the job echo "$list"|sed -r 's/[a-z0-9@_-]+.target//;s/ +/ /g;s/ /n/g'. This works in BusyBox 1.2.1, very old.
– Paulo
Dec 11 at 23:27














Thanks @Paulo for your response. Unfortunatelly, sed doesn't remove the suffix and the generate output is item1.service instead of simply item1 as I need.
– j4x
Dec 12 at 8:03




Thanks @Paulo for your response. Unfortunatelly, sed doesn't remove the suffix and the generate output is item1.service instead of simply item1 as I need.
– j4x
Dec 12 at 8:03




1




1




@j4x The sed you posted has the command to cut the suffix, just append it to sed script sed -r 's/[a-z0-9@_-]+.target//;s/.service//g;s/ +/ /g;s/ /n/g'. g flag is needed to replace all matches, without it sed will replace only the first match.
– Paulo
Dec 12 at 11:52






@j4x The sed you posted has the command to cut the suffix, just append it to sed script sed -r 's/[a-z0-9@_-]+.target//;s/.service//g;s/ +/ /g;s/ /n/g'. g flag is needed to replace all matches, without it sed will replace only the first match.
– Paulo
Dec 12 at 11:52














Good. IT is devilish to understand but works! @Paulo, please post an answer so I can mark it. Thanks!
– j4x
Dec 12 at 12:13




Good. IT is devilish to understand but works! @Paulo, please post an answer so I can mark it. Thanks!
– j4x
Dec 12 at 12:13










1 Answer
1






active

oldest

votes


















1














list='item1.service item2.service item3.target item4.service'
echo "$list" | sed -r '

# Since the input is only one line, all commands will scan all the pattern space,
# so the commands order matters.

# replace for nothing unwanted text
s/[a-z0-9@_-]+.target//

# replace for nothing unwanted suffix
# (with 'g' flag the command will replace all occurrences)
s/.service//g

# squeeze double spaces
s/ +/ /g

# replace space for new line character
s/ /n/g'


I think this will work in all versions of BusyBox's sed (could be awk too if you have it in your BusyBox).



I just downloaded the latest BusyBox version and ran make menuconfig but couldn't
find any reference to Perl regex.






share|improve this answer





















    Your Answer








    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "3"
    };
    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: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    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%2fsuperuser.com%2fquestions%2f1382676%2fbusybox-filter-string-list%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    1














    list='item1.service item2.service item3.target item4.service'
    echo "$list" | sed -r '

    # Since the input is only one line, all commands will scan all the pattern space,
    # so the commands order matters.

    # replace for nothing unwanted text
    s/[a-z0-9@_-]+.target//

    # replace for nothing unwanted suffix
    # (with 'g' flag the command will replace all occurrences)
    s/.service//g

    # squeeze double spaces
    s/ +/ /g

    # replace space for new line character
    s/ /n/g'


    I think this will work in all versions of BusyBox's sed (could be awk too if you have it in your BusyBox).



    I just downloaded the latest BusyBox version and ran make menuconfig but couldn't
    find any reference to Perl regex.






    share|improve this answer


























      1














      list='item1.service item2.service item3.target item4.service'
      echo "$list" | sed -r '

      # Since the input is only one line, all commands will scan all the pattern space,
      # so the commands order matters.

      # replace for nothing unwanted text
      s/[a-z0-9@_-]+.target//

      # replace for nothing unwanted suffix
      # (with 'g' flag the command will replace all occurrences)
      s/.service//g

      # squeeze double spaces
      s/ +/ /g

      # replace space for new line character
      s/ /n/g'


      I think this will work in all versions of BusyBox's sed (could be awk too if you have it in your BusyBox).



      I just downloaded the latest BusyBox version and ran make menuconfig but couldn't
      find any reference to Perl regex.






      share|improve this answer
























        1












        1








        1






        list='item1.service item2.service item3.target item4.service'
        echo "$list" | sed -r '

        # Since the input is only one line, all commands will scan all the pattern space,
        # so the commands order matters.

        # replace for nothing unwanted text
        s/[a-z0-9@_-]+.target//

        # replace for nothing unwanted suffix
        # (with 'g' flag the command will replace all occurrences)
        s/.service//g

        # squeeze double spaces
        s/ +/ /g

        # replace space for new line character
        s/ /n/g'


        I think this will work in all versions of BusyBox's sed (could be awk too if you have it in your BusyBox).



        I just downloaded the latest BusyBox version and ran make menuconfig but couldn't
        find any reference to Perl regex.






        share|improve this answer












        list='item1.service item2.service item3.target item4.service'
        echo "$list" | sed -r '

        # Since the input is only one line, all commands will scan all the pattern space,
        # so the commands order matters.

        # replace for nothing unwanted text
        s/[a-z0-9@_-]+.target//

        # replace for nothing unwanted suffix
        # (with 'g' flag the command will replace all occurrences)
        s/.service//g

        # squeeze double spaces
        s/ +/ /g

        # replace space for new line character
        s/ /n/g'


        I think this will work in all versions of BusyBox's sed (could be awk too if you have it in your BusyBox).



        I just downloaded the latest BusyBox version and ran make menuconfig but couldn't
        find any reference to Perl regex.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Dec 12 at 14:09









        Paulo

        52928




        52928






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Super User!


            • 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.





            Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


            Please pay close attention to the following guidance:


            • 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%2fsuperuser.com%2fquestions%2f1382676%2fbusybox-filter-string-list%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

            "Incorrect syntax near the keyword 'ON'. (on update cascade, on delete cascade,)

            Alcedinidae

            Origin of the phrase “under your belt”?