Tr

From LQWiki
Jump to: navigation, search

tr

tr is a command for translating or deleting characters. It takes a stream of input on stdin and outputs the translated stream on stdout.

Origin

Provided by the GNU Coreutils: man page

Tips

tr can be used to remove characters from a stream using the -d option.

  • Here's a quick way to convert a file with DOS end of lines (CR\LF) to UNIX end of lines:
tr -d '\015' < $FILE.dos > $FILE.unix
  • Here's an example how to delete a line feed, e.g. behind an echo (note you can also use echo -n to suppress a linefeed behind an echo command)
user@computer:> echo "hello world"
hello world
user@computer:> echo "hello world"|hexdump -C
00000000  68 65 6c 6c 6f 20 77 6f  72 6c 64 0a              |hello world.|
0000000c
user@computer:> echo "hello world"|tr -d '\n'|hexdump -C
00000000  68 65 6c 6c 6f 20 77 6f  72 6c 64                 |hello world|
0000000b
user@computer:> echo "hello world" | tr -d '\n'
hello worlduser@computer:>
  • Another handy trick is using tr to squeeze multiple characters into one, which is sometimes handy if you want to use cut to chop things up:
ls -l | tr -s ' '

(this turns multiple spaces into one. see the cut page for a reason why this is useful)

See also

expand, unexpand

External links