Help regarding behavior sed regexp query

Hi all,

I have one question regarding sed regexp (or any regexp in general),

I have some path like this

C:/Abc/def/ghi/jkl in a file file1

Now if i use following code

cat file1 | sed 's#\(.*\)/.*#\1#'

Now it give me following output
C:/Abc/def/ghi, which is fine

But i just want to know that in this sed code, why it is treating only last forward slash in file1 as the base for creating value of subsection (.*). i mean to say why it is treating C:/Abc/def/ghi as value of subsection. We are having 5 forward slashes in file1, why not it is picking upto C:/Abc/def.

Is that the property of regexp that if a sentence has five forward slashes & i use .*/.*, it will treat forward slash as last forward slash in file.

Can any one please suggest me some article/link to this question. I hope you are getting my question.

Thanks in advance

Sarbjit

Hi the regexp in sed is greedy, which means it will try to find the longest possible match.

UUOC

sed 's#\(.*\)/.*#\1#' file1

If you want to match only to the first slash, first match only those characters that are not slashes:

sed 's#\([!/]*\)/.*#\1#' file1