Need to strip a string

I have a file that looks like this:

/home/fred/opt/bin
/opt/usr/bin
/usr/sbin/var/opt

I need a way to chop of everything after the last occurance of the / sign including the /. So the file above will now look like this below.

/home/fred/opt
/opt/usr
/usr/sbin/var

I tried using a perl oneliner like this but I just can't get it.

perl -pi -w -e 's/\/{1}$//;' inputfile.txt

Thanks

-X96riley

for FILE in `cat aap`; do dirname $FILE; done

Ok. I'm retarded.

I made it so hard by doing this

perl -pi -w -e 's/\/[^\/]*$//;' inputfile.txt

Thanks sb008

-96

or without the UUOC or external command

while read file; do
    echo ${file%/*}
done < inputfile.txt

or with perl along the lines you already tried.

perl -pi -e "s/\/[^\/]*$/\n/" inputfile.txt

you can use dirname too, if you have it

[root@localhost test]# dirname /home/fred/opt/bin
/home/fred/opt

while that would work that would be inefficient in the extreme for a large number of directories, because it would need to fork and exec for each call line in the input file.

With bash:

set "$(<infile)"; printf "%s\n" "${@%/*}"