I am trying to make tr -d
remove a string of characters from an existing string, without it removing characters everywhere else.
For example, I want tr
to remove : OK
from the end of every string in foo.txt
.
Contents of foo.txt
:
BROKEN BONES: OK
Kefen Odvora: OK
BOOKS_FOR_MUM: OK
E: OK Amded: OK
This is the command I run:
cat foo.txt | tr -d ": OK$"
I want it to output this:
BROKEN BONES
Kefen Odvora
BOOKS_FOR_MUM
E: OK Amded
But instead I get this, which I don't want:
BRENBNES
efendvora
BS_FR_MUM
EAmded
How can I fix this?
You are using the wrong tool. You want sed
, not tr
:
cat foo.txt | sed 's/: OK$//'
or preferably
sed 's/: OK$//' foo.txt
when your input really is just a file, not a more complicated command.
tr -d
removes all occurrences of any character found in the argument to -d
; it does not treat it as a regular expression to match and remove. Specifically, you are removing all occurrences of :
, ,
O
, K
, and $
from each line.