Busybox filter string list
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:
- I need keep only the
.service
items. - 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
add a comment |
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:
- I need keep only the
.service
items. - 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
What does this have to do with Busybox?
– JakeGould
Dec 11 at 16:38
sed
does all the jobecho "$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 isitem1.service
instead of simplyitem1
as I need.
– j4x
Dec 12 at 8:03
1
@j4x Thesed
you posted has the command to cut the suffix, just append it tosed
scriptsed -r 's/[a-z0-9@_-]+.target//;s/.service//g;s/ +/ /g;s/ /n/g'
.g
flag is needed to replace all matches, without itsed
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
add a comment |
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:
- I need keep only the
.service
items. - 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
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:
- I need keep only the
.service
items. - 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
regex string-manipulation busybox
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 jobecho "$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 isitem1.service
instead of simplyitem1
as I need.
– j4x
Dec 12 at 8:03
1
@j4x Thesed
you posted has the command to cut the suffix, just append it tosed
scriptsed -r 's/[a-z0-9@_-]+.target//;s/.service//g;s/ +/ /g;s/ /n/g'
.g
flag is needed to replace all matches, without itsed
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
add a comment |
What does this have to do with Busybox?
– JakeGould
Dec 11 at 16:38
sed
does all the jobecho "$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 isitem1.service
instead of simplyitem1
as I need.
– j4x
Dec 12 at 8:03
1
@j4x Thesed
you posted has the command to cut the suffix, just append it tosed
scriptsed -r 's/[a-z0-9@_-]+.target//;s/.service//g;s/ +/ /g;s/ /n/g'
.g
flag is needed to replace all matches, without itsed
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
add a comment |
1 Answer
1
active
oldest
votes
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.
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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.
add a comment |
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.
add a comment |
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.
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.
answered Dec 12 at 14:09
Paulo
52928
52928
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
What does this have to do with Busybox?
– JakeGould
Dec 11 at 16:38
sed
does all the jobecho "$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 isitem1.service
instead of simplyitem1
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 tosed
scriptsed -r 's/[a-z0-9@_-]+.target//;s/.service//g;s/ +/ /g;s/ /n/g'
.g
flag is needed to replace all matches, without itsed
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