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
sb008
January 26, 2007, 4:23pm
2
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
reborg
January 26, 2007, 4:56pm
4
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
reborg
January 26, 2007, 9:47pm
6
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.
x96riley3:
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
[...]
With bash:
set "$(<infile)"; printf "%s\n" "${@%/*}"