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:
- 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.
What does this have to do with Busybox? – JakeGould – 2018-12-11T16:38:20.013
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 – 2018-12-11T23:27:47.017Thanks @Paulo for your response. Unfortunatelly,
sed
doesn't remove the suffix and the generate output isitem1.service
instead of simplyitem1
as I need. – j4x – 2018-12-12T08:03:57.4601@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 – 2018-12-12T11:52:08.690Good. IT is devilish to understand but works! @Paulo, please post an answer so I can mark it. Thanks! – j4x – 2018-12-12T12:13:32.283