TR Command 0x0

s4lt    12th Jan 2019   

/in

The tr (translate) command is used for translating, squeezing, and/or deleting characters from standard input and writing it to standard output. I'm going to attempt to break it down so that it is easier to understand.

/breakdown

Assuming you have an understanding of standard input, standard output, and redirections, the translate can help you take inputted information and manipulate it out to the command line or to a file for viewing at a later time.

One of the easiest ways to learn more about the translate command is to access the help option which will give you information on the various switches that can be used.


tr --help

To use the translate command, you'll type tr with or without an [OPTION], and then the set of characters you want to match in [SET1] and then replaced with [SET2]. If you do not select an option, by default you will replace any matches in SET1 with characters in SET2.

The first switch you are given is -c or -C which, according to the man page, will use the complement of SET1. That is a very vague explanation and leaves you wonderin what it means, but to make it simple, the output will be the exact number of characters in your output, but it will be replaced with whatever was matched in SET1 or filled in by the last character provided in SET2 if the character didn't match. So if you use the command like this:


echo what do you want | tr -c a-g 6

You will end up with the output of:


66a66d6666666a666

It also seems to add 1 extra character (no clue why). My output was 16 characters long (including spaces) and turned into a 17 character mess. The only characters that matched in SET1 were a and d, so all of the other characters were converted to the number 6. Makes sense why -c isn't a popular option when using the translate command.

The second option is -d or --delete which allows you to delete anything matched in SET1 from your output. You do not need SET2 when using the delete option. Here is an example:


echo what do you want | tr -d a-g

Outputs...


Wht o you wnt

The next option is the -s or --squeeze-repeats option. The squeeze option will allow you to remove repeated characters that are matched in SET1 so that you only have one occurence of that character. The characters have to be in sequence, otherwise they will not be replaced. SET2 is not required for this to work. Here is an example of how it is used:


echo whaat dod yoou waaant | tr -s a-g

Outputs...


What dod yoou want

You can see that the sequential 'a' characters were removed since they were matched in SET1, but the 'o' characters were not. Likewise, the 'dod' remained since there is an 'o' character in between the two 'd' characters.

The last option you are given is c-t or --truncate-set1 which is used to turncate SET1 to the length of SET2. This is probably the weirdest one to explain due to how the it actually truncates the output. It will actually start at the first occurence of a match, and then move from there. Here is the command in action:


echo What do you want | tr -t a-g blah

And the output...


Whbt ho you wbnt

You can see blah starts at the first 'a' character in the word 'What' and, since it is exactly 4 characters away, ends with the 'd' character in the word 'do'. There is only one other match, which is replaced by a 'b' character. If I had added a character from a through g to the end of the word 'want' like 'wantf', then it would have produced 'wbnth' much like the first and second match. If you had four consequtive matches, it would spell out the word 'blah' since that is the length of SET2.

/out

Well, this is my first attempt ever to try and explain something in a manner so that someone without prior knowledge of the translate command can easily pick up. To be quite honest, I didn't know much about the translate command myself, but this is supposedly a good way to learn.

If you have any gripes, or would like to correct me, shoot me an email over at info@saltwrx.org.



Tweet This!

Categories






Archive

Soon...


Social Media

       


Donate

     
HTB Badge