Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Match from beginning to word as long as there are no . in between: Convert grep -Po command to sed

Tags:

regex

grep

bash

sed

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.

like image 886
enio Avatar asked Apr 27 '21 14:04

enio


People also ask

How do you use sed to match word and perform find and replace?

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.

What is the difference between sed and grep?

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.

How do I match a whole word in grep?

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.

What does this sed command do?

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.


3 Answers

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
    sed:
  • -n - suppresses default line output in the sed command
  • s/^\([^.]*\)\.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 prints the resulting value.
like image 153
Wiktor Stribiżew Avatar answered Nov 11 '22 23:11

Wiktor Stribiżew


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 replacement
like image 37
anubhava Avatar answered Nov 12 '22 00:11

anubhava


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

like image 41
RavinderSingh13 Avatar answered Nov 12 '22 00:11

RavinderSingh13