SED with Wildcards and Special Characters

Hi All,

Need you guys' help to achieve the following:

I have some strings and i wish to threw off the end part that's in the file path.

From:
/directoryname1/subdirectoryname1/abc.txt
/directoryname2/subdirectoryname2/defggf.txt

To:
/directoryname1/subdirectoryname1/
/directoryname2/subdirectoryname2/

I could not trigger the beginning of the string as they varies. Right now,
I'm using SED to try and catch /*.txt and repalce it with nothing but i don't seem to get it to work.

$path = /var/tmp/123.txt
echo $path | sed 's/\/*\.txt//'

Outputs:
/var/tmp/123

$path = /var/tmp/123.txt
echo $path | sed 's/\/\*txt//'

Outputs:
/var/tmp/123.txt (does nothing)

I'm just tyring to replace "/*.txt" to " "(nothing). Hope you guys can help me on that.
Thanks in advance.

one way

# a=/directoryname1/subdirectoryname1/abc.txt
# echo ${a%*/*}
/directoryname1/subdirectoryname1

or, your sed command can be revised to the following:
echo $path | sed 's/\/[^\/]*txt$//'

many thanks cuitao, mind to explain
[^\/]*txt$ part?

isn't what everything inside the means that it is not equal to [/] and what is the dollar sign for?

Thanks
:b:

your guess about the is correct. and the $ represents the end of the line.

Look like the OP need the ending slash.

$ echo '/directoryname2/subdirectoryname2/defggf.txt' | awk -F'/' '{gsub($NF,"",$0)}1'
/directoryname2/subdirectoryname2/

The other solutions end like dirname,see basename(1).

$ a=/directoryname2/subdirectoryname2/defggf.txt
$ dirname $a
/directoryname2/subdirectoryname2

another

echo '/directoryname2/subdirectoryname2/defggf.txt' | awk -F'/' '{$NF=""}1' OFS="/"

@ghostdog74 Nice :b:
Same like yours:

echo '/directoryname2/subdirectoryname2/defggf.txt' | awk  'BEGIN{FS=OFS="/"}{$NF=""}1'