How to use tr to replace string '-'
The best you can do with tr
it is to remove the hyphens leaving blank lines. The best way to do what you need is Amro's answer with help sed
. It is important to remember that you tr
are dealing with lists of characters, not multi-character strings, so there is no point in inserting two hyphens in your parameters.
$ tr -d "-" textfile
out1
out2
out3
However, in order for the descriptor to tr
handle hyphens and additional characters, you need to terminate the parameters with --
or place a hyphen after the first character. Let's say you want to get rid of the hyphen and letter-o:
$ tr -d "-o" textfile
tr: invalid option -- 'o'
Try `tr --help' for more information.
$ tr -d -- "-o" textfile
ut1
ut2
ut3
$ tr -d "o-" textfile
ut1
ut2
ut3
It is often useful to use an option terminator --
when the character list is in a variable, so bad data does not create errors unnecessarily. This is true for teams other than tr
.
tr -d -- $charlist $file
source to share