Tr

From Hackepedia
Jump to navigationJump to search

tr stands for translate characters and that's what it does.

tr and changing /dev/zero

If you want a stream of characters like /dev/zero, tr can help you out. Pretend this:

francisco$ dd if=/dev/zero count=1 | tr '\000' '\377'| hexdump -C
1+0 records in
1+0 records out
512 bytes transferred in 0.000 secs (579841 bytes/sec)
00000000  ff ff ff ff ff ff ff ff  ff ff ff ff ff ff ff ff  |ÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿ|
*
00000200

As one can see the stream from 0x0 has changed to 0xff.


tr and sectioning numerics

Pretend you want to see all numbers in a document and count them then here is a tr syntax:

francisco$ tr -c -d '0-9' < testprog.c | hexdump -C       
00000000  38 31 39 32 30 30 30 31  38 31 39 32 38 31 39 32  |8192000181928192|
00000010  38 31 39 32 31 30 30 30  30                       |819210000|
00000019
francisco$ 

Only numbers are outputted from a .c program file.


tr and removing newlines

francisco$ more file
line1
line2
line3
francisco$ tr -d '\n' < file | hexdump -C
00000000  6c 69 6e 65 31 6c 69 6e  65 32 6c 69 6e 65 33     |line1line2line3|
0000000f