user13846961
user13846961

Reputation: 55

Bash sed non-greedy matching

Here is my text:

1a.begin /path/1a.file
2bx.begin2 /path/my/2bx.file2

and the expected output is

begin /path/1a.file
begin2 /path/my/2bx.file2

Here I want to do it by using non-greedy matching by sed. (The default matching by sed is greedy and all the 1a. and 2bx. will be removed)

Hence I tried the command:

echo -e "1a.begin /path/1a.file\n2bx.begin2 /path/my/2bx.file2"|sed 's/$.*[^\.]\.//g'

where I used the $.* to match all strings starting at the head of a line. I used [^\.] to prevent greedy matching all . in a line (see similar method in https://www.unix.com/shell-programming-and-scripting/133641-non-greedy-sed.html) But it did not change the text.

So where is my script wrong?

Upvotes: 3

Views: 551

Answers (1)

sseLtaH
sseLtaH

Reputation: 11227

  • Your start of line anchor $ is wrong, you should be using ^
  • You use a greedy match .* up to the last period .

Using sed

$ sed 's/^[^.]*\.//' input_file
begin /path/1a.file
begin2 /path/my/2bx.file2

Upvotes: 8

Related Questions