by Oliver; 2014
60. tr
tr stands for
translate and it's a utility for replacing characters in text.
For example, to replace a period with a newline:
$ echo "joe.joe" | tr "." "\n"
joe
joe
Change lowercase letters to uppercase letters:
$ echo joe | tr "[:lower:]" "[:upper:]"
JOE
Find the numberings of columns in a header (produced by the bioinformatics program
BLAST):
$ cat blast_header
qid sid pid alignmentlength mismatches numbergap query_start query_end subject_start subject_end evalue bitscore
$ cat blast_header | tr "\t" "\n" | nl -b a
1 qid
2 sid
3 pid
4 alignmentlength
5 mismatches
6 numbergap
7 query_start
8 query_end
9 subject_start
10 subject_end
11 evalue
12 bitscore
tr, with the
-d flag, is also useful for deleting characters.
If we have a file
tmp.txt:
$ cat tmp.txt
a a a a
a b b b
a v b b
1 b 2 3
then:
$ cat tmp.txt | tr -d "b"
a a a a
a
a v
1 2 3
This is one of the easiest ways to delete newlines from a file:
$ cat tmp.txt | tr -d "\n"
a a a aa b b ba v b b1 b 2 3
Tip: To destroy carriage return characters ("\r"), often seen when you open a Windows file in linux, use:
$ cat file.txt | tr -d "\r"
<PREV
NEXT>