bashsednon-greedy

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?


Solution

  • Using sed

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