Issue
I have made the following command to be able to match the string from the beginning of the line until the first occurrence of ".enabled" as long as there are no "." in between.
grep -Po '^\K[\w-]*?(?=\.enabled)'
input:
a-b-c.a.enabled.xxx.xx
a-b-c.a.b.enabled.xxx.xx
a-b-c.enabled.xxx.xx
output:
a-b-c
It runs properly on my local env with grep v3.1 but on Busybox v1.28.4 it says "grep: unrecognized option: P"
For that reason, I would like to convert this command to sed. Any input would be really helpful.
Solution
You can use
awk -F'.' '$2 == "enabled"{print $1}' file
sed -n 's/^\([^.]*\)\.enabled.*/\1/p' file
See the online demo.
Details:
awk:
-F'.'
- the field separator is set to a.
$2 == "enabled"
- if Group 2 value isenabled
, then{print $1}
- print Field 1 value
sed:-n
- suppresses default line output in thesed
commands/^\([^.]*\)\.enabled.*/\1/p
- finds any zero or more chars other than.
at the start of string (placing them into Group 1,\1
), then a.enabled
and then the rest of the string and replaces with the Group 1 value, andp
rints the resulting value.
Answered By - Wiktor Stribiżew Answer Checked By - Mary Flores (WPSolving Volunteer)