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.
Find and replace text within a file using sed command Use Stream EDitor (sed) as follows: sed -i 's/old-text/new-text/g' input.txt. The s is the substitute command of sed for find and replace. It tells sed to find all occurrences of 'old-text' and replace with 'new-text' in a file named input.txt.
The sed command is a stream editor that works on streams of characters. It's a more powerful tool than grep as it offers more options for text processing purposes, including the substitute command, which sed is most commonly known for.
The easiest of the two commands is to use grep's -w option. This will find only lines that contain your target word as a complete word. Run the command "grep -w hub" against your target file and you will only see lines that contain the word "hub" as a complete word.
The sed command, short for stream editor, performs editing operations on text coming from standard input or a file. sed edits line-by-line and in a non-interactive way. This means that you make all of the editing decisions as you are calling the command, and sed executes the directions automatically.
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 is enabled
, then{print $1}
- print Field 1 value-n
- suppresses default line output in the sed
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, and p
rints the resulting value.You may use this equivalent sed
of your grep -P
command:
sed -nE 's/^([-_[:alnum:]]+)\.enabled.*/\1/p' file
a-b-c
Details:
-n
: Suppress notmal output-E
: Enables extended regex mode([-_[:alnum:]]+)
: -_[:alnum:]]
is equivalent of [-\w]
or [-_a-zA-Z0-9]
. It matches 1+ of these characters and captures them in group #1\.enabled.*
: matches .enabled
followed by 0 or more of any string\1
: is replacement string that put value captured in capture group #1 back in replacementWith your shown samples, you could try following.
awk -F'\\.enabled' '$1~/^[-_[:alnum:]]+$/{print $1}' Input_file
Explanation: Simply making field separator as .enabled
for all the lines here. Then in main program checking condition if 1st field is having -
-or
_` or alphanumeric then print 1st field here.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With