Regex extracting location

I have location as

line=/got/cpp/track/tmp/piy/p_tok.xml

I need the path till tmp i.e. /got/cpp/track/tmp

i tried

test=${line%/)}

and i am getting location till

/got/cpp/track/tmp/piy

Can anyone help me in correcting my regex

Hi, try:

test=${line%/*/*}

It is not a regex, but a Unix pattern

Another approach is to get the bit you want to delete into a variable then trim that from line like this:

$ line=/got/cpp/track/tmp/piy/p_tok.xml
$ del=${line##*/tmp/}
$ echo $del
piy/p_tok.xml
$  echo ${line%/$del}
/got/cpp/track/tmp

or as a 1 liner:

$ echo ${line%/${line##*/tmp/}}
/got/cpp/track/tmp