Removing hyphen from beginning and end of a word only.

It is very simple to remove a hyphen from a word anywhere in that word using a simple sed command (sed -i 's/-//g' filename), but I am not able to figure out how to do this:

For example,

apple
-orange
tree
pipe-
banana-shake
dupe-

What my output should look like:

apple
orange
tree
pipe
banana-shake
dupe

I want to do this in Linux with BASH.

You can also use sed for the task:

sed -e 's/^-//' -e 's/-$//' filename
1 Like