Delete last character in a word but only if the character is there - in bash
You just need to escape the dot in your sed
command and everything will be fine.
Like this:
sed 's/\.$//'
Because in the case you don't escape it, .$
will match to any character at the end of string.
Also you can put all your sed
+ grep
+ cut
into just one sed
:
sed 's=/[^/]*$==;s/\.$//' filename
Removing a character only if it is there is exactly the description of parameter expansions.
$ var=path.
$ echo "$var ${var%.}"
path. path
The dot is not special in this case (a dot is special in a regex).
The other pattern could be removed with %/*
:
$ var=openOffice.org/ozm
$ echo "${var%/*}"
openOffice.org
To remove both:
$ var=roonstr./ozm
$ var=${var%/*}
$ var=${var%.}
$ echo "$var"
roonstr
Of course, to work with a source file it is faster to use sed on the file.
Only remember to quote the dot (to match it literally, otherwise it means: any character).
$ sed 's,/.*,,;s,\.$,,' file